ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
plot_shape.h
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2017-2020 German Aerospace Center (DLR).
3  * Eclipse ADORe, Automated Driving Open Research https://eclipse.org/adore
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License 2.0 which is available at
7  * http://www.eclipse.org/legal/epl-2.0.
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  * Robert Markowski
13  ********************************************************************************/
14 #pragma once
15 
17 #include <adore/mad/adoremath.h>
19 
20 namespace adore
21 {
22 
23 namespace PLOT
24 {
25 
26 inline void plotPosition(std::string tag, double x, double y, DLR_TS::PlotLab::AFigureStub* figure,
27  std::string options="LineColor=0,0,0;LineWidth=0;FillColor=1,0,0", double r=0.5)
28 {
29  const int numPoints = 16;
30 
31  static double cosAngle[numPoints],sinAngle[numPoints];
32 
33  if(cosAngle[1] == 0.0)
34  {
35  for(int i = 0; i<numPoints; i++)
36  {
37  double angle = 2.0*M_PI*i/numPoints;
38  cosAngle[i] = cos(angle);
39  sinAngle[i] = sin(angle);
40  }
41  }
42 
43  double X[numPoints], Y[numPoints], Z[numPoints];
44  for(int i = 0; i<numPoints; i++)
45  {
46  X[i] = x+cosAngle[i]*r;
47  Y[i] = y+sinAngle[i]*r;
48  Z[i] = 1;
49  }
50  figure->patch(tag,X,Y,Z,numPoints,options);
51 }
52 
53 inline void plotCircle(std::string tag, double x, double y, double z, double r, DLR_TS::PlotLab::AFigureStub* figure,
54  std::string options="LineColor=1,0,0;LineWidth=1")
55 {
56  const int numPoints = 16;
57 
58  static double cosAngle[numPoints],sinAngle[numPoints];
59 
60  if(cosAngle[1] == 0.0)
61  {
62  for(int i = 0; i<numPoints; i++)
63  {
64  double angle = 2.0*M_PI*i/numPoints;
65  cosAngle[i] = cos(angle);
66  sinAngle[i] = sin(angle);
67  }
68  }
69 
70  double X[numPoints+1], Y[numPoints+1], Z[numPoints+1];
71  for(int i = 0; i<numPoints; i++)
72  {
73  X[i] = x+cosAngle[i]*r;
74  Y[i] = y+sinAngle[i]*r;
75  Z[i] = z;
76  }
77  X[numPoints]=X[0];
78  Y[numPoints]=Y[0];
79  Z[numPoints]=Z[0];
80  figure->plot(tag,X,Y,Z,numPoints+1,options);
81 }
82 
83 inline void plotCylinder(std::string tag, double x, double y, double z0, double z1, double r, double max_z_diff, DLR_TS::PlotLab::AFigureStub* figure,
84  std::string options="LineColor=1,0,0;LineWidth=1")
85 {
86  int i = 0;
87  double h = 0;
88  do
89  {
90  // create tag
91  std::stringstream ss;
92  ss << tag << "/n" << i;
93 
94  // calculate z-coordinate of circle, cap at z1
95  h = std::min(z0+i*max_z_diff,z1);
96 
97  // plot circle
98  plotCircle(ss.str(),x,y,h,r,figure,options);
99  i++;
100  } while (h < z1);
101 }
102 
103 inline void plotRectangle(std::string tag, double x, double y, double l, double w,
104  DLR_TS::PlotLab::AFigureStub* figure, std::string options="LineColor=0,0,0;LineWidth=0;FillColor=1,0,0", double alpha=0.0)
105 {
106  double pi = 3.141592654;
107  double r;
108  const int numOfEdges = 4;
109  const int numPoints = 13;
110  double x_rec[numPoints];
111  double y_rec[numPoints];
112  double z_rec[numPoints];
113  int cc=0;
114  double a=w/2.0;
115  double b=l/2.0;
116  double theta;
117  double edges [numOfEdges]={atan2(a,b),atan2(a,-b),atan2(-a,-b)+2*pi ,atan2(-a,b)+2*pi};
118  double points[numPoints] = {atan2(a/2,b),atan2(a,b),atan2(a,b/2),atan2(a,-b/2), atan2(a,-b),atan2(a/2,-b),atan2(-a/2,-b)+2*pi,atan2(-a,-b)+2*pi,atan2(-a,-b/2)+2*pi,atan2(-a,b/2)+2*pi,atan2(-a,b)+2*pi ,atan2(-a/2,b)+2*pi ,atan2(a,b)};
119 
120  for (int i=0; i<numPoints;i++)
121  {
122  theta = points[i];
123  if( (theta>=0 && theta<edges[0]) || (theta>=edges[3] && theta<2*pi ) )
124  {
125  r=b/cos(theta);
126  x_rec[cc] = r*cos(theta+alpha);
127  y_rec[cc] = r*sin(theta+alpha);
128  x_rec[cc] = x_rec[cc] + x;
129  y_rec[cc] = y_rec[cc] + y;
130  z_rec[cc] = 1.0;
131  cc=cc+1;
132  }
133  else if( theta>= edges[0] && theta < edges[1] )
134  {
135  r= a/cos(-pi/2+(theta));
136  x_rec[cc] = r*cos(theta+alpha);
137  y_rec[cc] = r*sin(theta+alpha);
138  x_rec[cc] = x_rec[cc] + x;
139  y_rec[cc] = y_rec[cc] + y;
140  z_rec[cc] = 1.0;
141  cc=cc+1;
142  }
143  else if ( theta>= edges[1] && theta < edges[2] )
144  {
145  r= b/cos(-pi+(theta));
146  x_rec[cc] = r*cos(theta+alpha);
147  y_rec[cc] = r*sin(theta+alpha);
148  x_rec[cc] = x_rec[cc] + x;
149  y_rec[cc] = y_rec[cc] + y;
150  z_rec[cc] = 1.0;
151  cc=cc+1;
152  }
153  else if ( theta>= edges[2] && theta < edges[3] )
154  {
155  r= a/cos(pi/2+(theta));
156  x_rec[cc] = r*cos(theta+alpha);
157  y_rec[cc] = r*sin(theta+alpha);
158  x_rec[cc] = x_rec[cc] + x;
159  y_rec[cc] = y_rec[cc] + y;
160  z_rec[cc] = 1.0;
161  cc=cc+1;
162  }
163  }
164  figure->plot(tag,x_rec,y_rec,z_rec,numPoints,options);
165 }
166 inline void unPlotRectangle(std::string tag,DLR_TS::PlotLab::AFigureStub* figure)
167 {
168  figure->erase(tag);
169 
170 }
171 inline void plotArrow(std::string hashtag,double x,double y,double z,double alpha,double shaft_length,
172  double head_length,std::string options,DLR_TS::PlotLab::AFigureStub* figure)
173 {
174  double pi = 3.141592654;
175  double head_angle = pi/4.0;
176  double ca = std::cos(alpha);
177  double sa = std::sin(alpha);
178  double tail_length = head_length*0.66;
179  double X[7];
180  double Y[7];
181  double x1 = x + shaft_length * ca;
182  double y1 = y + shaft_length * sa;
183 
184 
185  X[0] = x - tail_length * sa;//left tail
186  Y[0] = y + tail_length * ca;
187 
188  X[1] = x + tail_length * sa;//right tail
189  Y[1] = y - tail_length * ca;
190 
191  X[2] = x;//base
192  Y[2] = y;
193 
194  X[3] = x1;//tip
195  Y[3] = y1;
196 
197  X[4] = x1 + head_length * std::cos(alpha + pi - head_angle);//left
198  Y[4] = y1 + head_length * std::sin(alpha + pi - head_angle);
199 
200  X[5] = x1;//tip
201  Y[5] = y1;
202 
203  X[6] = x1 + head_length * std::cos(alpha - pi + head_angle);//right
204  Y[6] = y1 + head_length * std::sin(alpha - pi + head_angle);
205 
206  figure->plot(hashtag,X,Y,z,7,options);
207 }
208 
209 inline void plotLine(std::string hashtag,double x0,double y0,double x1,double y1,double z,std::string options,DLR_TS::PlotLab::AFigureStub* figure)
210 {
211  double X[2];
212  double Y[2];
213 
214  X[0] = x0;
215  X[1] = x1;
216 
217  Y[0] = y0;
218  Y[1] = y1;
219 
220  figure->plot(hashtag,X,Y,z,2,options);
221 }
222 inline void plotArrow(std::string hashtag,double x0,double y0,double z0,double x1,double y1,double shaft_length,double head_length,std::string options,DLR_TS::PlotLab::AFigureStub* figure)
223 {
224  double max_length_ratio = 1.0;
225  double max_width_ratio = 0.33;
226  double alpha = atan2(y1-y0,x1-x0);
227  double length = std::sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0));
228  shaft_length = std::min(shaft_length,length*max_length_ratio);
229  head_length = std::min(head_length,shaft_length*max_width_ratio);
230  double front = (length-shaft_length)/2.0;
231  plotArrow(hashtag,x0+front*std::cos(alpha),y0+front*std::sin(alpha),z0,alpha,shaft_length,head_length,options,figure);
232 }
233 
234 inline void plotVectorField(std::string hashtag,double* X,double* Y,double* dX,double* dY,double z0,int size,std::string options,DLR_TS::PlotLab::AFigureStub* figure)
235 {
236  for(int i=0;i<size;i++)
237  {
238  std::stringstream s;
239  s<<hashtag<<"/"<<i;
240  double L = std::sqrt(dX[i]*dX[i]+dY[i]*dY[i]);
241  plotArrow(s.str(),X[i],Y[i],z0,X[i]+dX[i],Y[i]+dY[i],L,0.33*L,options,figure);
242  }
243 }
244 
245 const void plotTrajectory(std::string name, const adore::fun::SetPointRequest * const trajectory, std::string options,DLR_TS::PlotLab::AFigureStub* figure, double z = 0.0)
246 {
247  static const size_t n_plot = 100;
248  double X[n_plot];
249  double Y[n_plot];
250  size_t count=0;
251  // int items = std::min(n_plot,trajectory->setPoints.size());
252  for (auto & point : trajectory->setPoints)
253  {
254  X[count] = point.x0ref.getX();
255  Y[count] = point.x0ref.getY();
256  count++;
257  if (count > n_plot) break;
258  }
259  figure->plot(name, X,Y,z,count,options);
260 }
261 
262 inline void plotPath(std::string name,const adoreMatrix<double>& data,double z, std::string options,DLR_TS::PlotLab::AFigureStub* figure)
263 {
264  static const int n_plot = 100;
265  double X[100];
266  double Y[100];
267  int batch=0;
268  for(int count=0;count<data.nc();count+=n_plot,batch++)
269  {
270  int items = (std::min)(n_plot,(int)data.nc()-count);
271  for(int i=0;i<items;i++)
272  {
273  X[i] = data(1,count+i);
274  Y[i] = data(2,count+i);
275  }
276  std::stringstream sbuf1;
277  sbuf1<<name<<"/"<<batch;
278  figure->plot(sbuf1.str(), X,Y,z,items,options);
279  }
280 }
281 inline void plotData(std::string name,const adoreMatrix<double>& data,double z,int d1,int d2,std::string options,DLR_TS::PlotLab::AFigureStub* figure)
282 {
283  static const int n_plot = 100;
284  double X[100];
285  double Y[100];
286  int batch=0;
287  for(int count=0;count<data.nc();count+=n_plot,batch++)
288  {
289  int items = (std::min)(n_plot,(int)data.nc()-count);
290  for(int i=0;i<items;i++)
291  {
292  X[i] = data(d1,count+i);
293  Y[i] = data(d2,count+i);
294  }
295  std::stringstream sbuf1;
296  sbuf1<<name<<"/"<<batch;
297  figure->plot(sbuf1.str(), X,Y,z,items,options);
298  }
299 }
300 
301 } // end PLOT namespace
302 
303 } // end adore namespace
#define M_PI
Definition: arraymatrixtools.h:24
Definition: afigurestub.h:24
virtual void patch(std::string hashtag, double *X, double *Y, double *Z, int size, std::string options)=0
virtual void plot(std::string hashtag, double *X, double *Y, double *Z, int size, std::string options)=0
virtual void erase(std::string hashtag)=0
Definition: setpointrequest.h:35
std::vector< SetPoint > setPoints
Definition: setpointrequest.h:38
void plotVectorField(std::string hashtag, double *X, double *Y, double *dX, double *dY, double z0, int size, std::string options, DLR_TS::PlotLab::AFigureStub *figure)
Definition: plot_shape.h:234
void plotArrow(std::string hashtag, double x, double y, double z, double alpha, double shaft_length, double head_length, std::string options, DLR_TS::PlotLab::AFigureStub *figure)
Definition: plot_shape.h:171
void unPlotRectangle(std::string tag, DLR_TS::PlotLab::AFigureStub *figure)
Definition: plot_shape.h:166
void plotCircle(std::string tag, double x, double y, double z, double r, DLR_TS::PlotLab::AFigureStub *figure, std::string options="LineColor=1,0,0;LineWidth=1")
Definition: plot_shape.h:53
void plotData(std::string name, const adoreMatrix< double > &data, double z, int d1, int d2, std::string options, DLR_TS::PlotLab::AFigureStub *figure)
Definition: plot_shape.h:281
void plotRectangle(std::string tag, double x, double y, double l, double w, DLR_TS::PlotLab::AFigureStub *figure, std::string options="LineColor=0,0,0;LineWidth=0;FillColor=1,0,0", double alpha=0.0)
Definition: plot_shape.h:103
const void plotTrajectory(std::string name, const adore::fun::SetPointRequest *const trajectory, std::string options, DLR_TS::PlotLab::AFigureStub *figure, double z=0.0)
Definition: plot_shape.h:245
void plotCylinder(std::string tag, double x, double y, double z0, double z1, double r, double max_z_diff, DLR_TS::PlotLab::AFigureStub *figure, std::string options="LineColor=1,0,0;LineWidth=1")
Definition: plot_shape.h:83
void plotLine(std::string hashtag, double x0, double y0, double x1, double y1, double z, std::string options, DLR_TS::PlotLab::AFigureStub *figure)
Definition: plot_shape.h:209
void plotPath(std::string name, const adoreMatrix< double > &data, double z, std::string options, DLR_TS::PlotLab::AFigureStub *figure)
Definition: plot_shape.h:262
void plotPosition(std::string tag, double x, double y, DLR_TS::PlotLab::AFigureStub *figure, std::string options="LineColor=0,0,0;LineWidth=0;FillColor=1,0,0", double r=0.5)
Definition: plot_shape.h:26
interval< T > atan2(interval< T > y, interval< T > x)
Definition: intervalarithmetic.h:234
interval< T > cos(interval< T > x)
Definition: intervalarithmetic.h:225
T min(T a, T b, T c, T d)
Definition: adoremath.h:663
interval< T > sin(interval< T > x)
Definition: intervalarithmetic.h:204
x0
Definition: adore_set_goal.py:25
x
Definition: adore_set_goal.py:30
y0
Definition: adore_set_goal.py:26
z0
Definition: adore_set_goal.py:27
y
Definition: adore_set_goal.py:31
z
Definition: adore_set_goal.py:32
y1
Definition: adore_set_pose.py:29
x1
Definition: adore_set_pose.py:28
z1
Definition: adore_set_pose.py:30
w
Definition: adore_set_pose.py:40
r
Definition: adore_suppress_lanechanges.py:209
Definition: areaofeffectconverter.h:20