ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
r2sauxiliary.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 - initial API and implementation
13  ********************************************************************************/
14 
15 #pragma once
16 #include <vector>
17 #include <tuple>
18 #include <algorithm>
21 #include <adore/mad/csvlog.h>
23 
24 namespace adore
25 {
26  namespace if_r2s
27  {
28  typedef std::vector<adore::env::BorderBased::Coordinate> TR2SGeometry;
29 
35  {
36  enum LINETYPE
37  {
40  };
41  int id_;
43  bool oneway_;
45 
47  {
48  id_=-1;
49  oneway_=false;
50  linetype_ = LINETYPE::StandardLine;
51  }
52  bool isSane() const
53  {
54  return geometry_.size()>1 && getBorderID().distance()>0.1;
55  }
56 
58  {
59  return adore::env::BorderBased::BorderID(*geometry_.begin(),*geometry_.rbegin());
60  }
62  {
63  return adore::env::BorderBased::BorderID(*geometry_.rbegin(),*geometry_.begin());
64  }
66  {
67  //filter distinct points
68  std::vector<int> distinct;
69  double tol = 0.05;
70  distinct.push_back(0);
71  for(int i=1;i<geometry_.size();i++)
72  {
73  if(geometry_[i].distance(geometry_[*distinct.rbegin()])>tol)distinct.push_back(i);
74  }
75 
76  //collect distinct points in path
77  adore::mad::function_type_xyz* path = new adore::mad::function_type_xyz(distinct.size(),0.0);
78  double s=0.0;
79  auto ci = geometry_[distinct[0]];
80  for(int i = 0;i<distinct.size();i++)
81  {
82  auto cj = geometry_[distinct[i]];
83  s+= ci.distance(cj);
84  path->getData()(0,i) = s;
85  path->getData()(1,i) = cj.m_X;
86  path->getData()(2,i) = cj.m_Y;
87  path->getData()(3,i) = cj.m_Z;
88  ci = cj;
89  }
90  return path;
91  }
93  {
94  //filter distinct points
95  std::vector<int> distinct;
96  double tol = 0.05;
97  distinct.push_back(geometry_.size()-1);
98  for(int i=geometry_.size()-2;i>=0;i--)
99  {
100  if(geometry_[i].distance(geometry_[*distinct.rbegin()])>tol)distinct.push_back(i);
101  }
102 
103  //collect distinct points in path
104  adore::mad::function_type_xyz* path = new adore::mad::function_type_xyz(distinct.size(),0.0);
105  double s=0.0;
106  auto ci = geometry_[distinct[0]];
107  for(int i = 0;i<distinct.size();i++)
108  {
109  auto cj = geometry_[distinct[i]];
110  s+= ci.distance(cj);
111  path->getData()(0,i) = s;
112  path->getData()(1,i) = cj.m_X;
113  path->getData()(2,i) = cj.m_Y;
114  path->getData()(3,i) = cj.m_Z;
115  ci = cj;
116  }
117  return path;
118  }
120  {
121  auto border = new adore::env::BorderBased::Border(
122  getBorderID(),
123  getBorderPath()
124  );
126  return border;
127  }
129  {
130  auto border = new adore::env::BorderBased::Border(
133  );
135  return border;
136  }
137  };
142  struct LaneBorder
143  {
144  enum TYPE
145  {
153  OTHER
154  };
155  int id_;
160  {
161  id_=-1;
162  parent_id_=-1;
163  type_=TYPE::NONE;
164  }
165  bool isSane() const
166  {
167  return geometry_.size()>1 && getBorderID().distance()>0.1;
168  }
170  {
171  return adore::env::BorderBased::BorderID(*geometry_.begin(),*geometry_.rbegin());
172  }
173  bool isOnRightHandSide(const ReferenceLine& rl)const
174  {
176  return myID.distance(rl.getBorderID())<=myID.distance(rl.getInverseBorderID());
177  }
178  double sortingDistance(const ReferenceLine& rl) const
179  {
181  if(isOnRightHandSide(rl))
182  {
183  return -myID.distance(rl.getBorderID());
184  }
185  else
186  {
187  return myID.distance(rl.getInverseBorderID());
188  }
189  }
190  double distance(const ReferenceLine& rl) const
191  {
193  return std::min(myID.distance(rl.getBorderID()),myID.distance(rl.getInverseBorderID()));
194  }
196  {
197  //filter distinct points
198  std::vector<int> distinct;
199  double tol = 0.05;
200  distinct.push_back(0);
201  for(int i=1;i<geometry_.size();i++)
202  {
203  if(geometry_[i].distance(geometry_[*distinct.rbegin()])>tol)distinct.push_back(i);
204  }
205 
206  //collect distinct points in path
207  adore::mad::function_type_xyz* path = new adore::mad::function_type_xyz(distinct.size(),0.0);
208  double s=0.0;
209  auto ci = geometry_[distinct[0]];
210  for(int i = 0;i<distinct.size();i++)
211  {
212  auto cj = geometry_[distinct[i]];
213  s+= ci.distance(cj);
214  path->getData()(0,i) = s;
215  path->getData()(1,i) = cj.m_X;
216  path->getData()(2,i) = cj.m_Y;
217  path->getData()(3,i) = cj.m_Z;
218  ci = cj;
219  }
220  return path;
221  }
223  {
225  getBorderID(),
226  getBorderPath()
227  );
230  border->m_left = new adore::env::BorderBased::BorderID(leftID);
231  return border;
232  }
233  };
234 
235  typedef std::vector<ReferenceLine> TReferenceLineVector;
236  typedef std::vector<LaneBorder> TLaneBorderVector;
238  typedef std::pair<TGeometryFunction*,LaneBorder::TYPE> TFunctionTypePair;
243  class Section
244  {
250  {
251  private:
252  std::vector<TFunctionTypePair> functions_;
253 
254  std::vector<TFunctionTypePair> getFunctionsStartingAtPoint(double x, double y)
255  {
256  std::vector<TFunctionTypePair> value;
257  for(auto it = functions_.begin(); it!=functions_.end(); it++)
258  {
259  auto fun = (*it).first;
260  if(fun->f(fun->limitLo())(0)==x && fun->f(fun->limitLo())(1)==y)
261  {
262  value.push_back(*it);
263  }
264  }
265  return value;
266  }
267  std::vector<TFunctionTypePair> getFunctionsEndingAtPoint(double x, double y)
268  {
269  std::vector<TFunctionTypePair> value;
270  for(auto it = functions_.begin(); it!=functions_.end(); it++)
271  {
272  auto fun = (*it).first;
273  if(fun->f(fun->limitHi())(0)==x && fun->f(fun->limitHi())(1)==y)
274  {
275  value.push_back(*it);
276  }
277  }
278  return value;
279  }
281  {
282  auto c1 = env::BorderBased::Coordinate(geometry->getData()(1,0),geometry->getData()(2,0),0);
283  geometry->getData()(0,0) = 0.0;
284  for(int i = 1; i<geometry->getData().nc(); i++)
285  {
286  auto c2 = env::BorderBased::Coordinate(geometry->getData()(1,i),geometry->getData()(2,i),0);
287  geometry->getData()(0,i) = geometry->getData()(0,i-1) + c1.distance(c2);
288  c1 = c2;
289  }
290  }
291  public:
299  {
300  functions_.push_back(std::make_pair(function,type));
301  }
308  std::vector<TFunctionTypePair> getFunctionsAtParameter(double x)
309  {
310  std::vector<TFunctionTypePair> value;
311  for(auto it = functions_.begin(); it!=functions_.end(); it++)
312  {
313  auto fun = (*it).first;
314  if(fun->limitLo() <= x && fun->limitHi() >= x)
315  {
316  value.push_back(*it);
317  }
318  }
319  return value;
320  }
326  std::vector<TFunctionTypePair> getFunctions()
327  {
328  return functions_;
329  }
336  void mend(TGeometryFunction* refLine, std::set<double>& intervals)
337  {
338  double x0 = refLine->limitLo();
339  double x1 = refLine->limitHi();
340  // if there's only one function, it's likely it goes over the whole length of the reference line
341  if(functions_.size()==1)
342  {
343  auto fun = functions_.at(0).first;
344  if(x0!=fun->limitLo() || x1!=fun->limitHi())
345  {
346  fun->stretchDomain(x0,x1);
347  intervals.insert(x0);
348  intervals.insert(x1);
349  }
350  return;
351  }
352  if(intervals.size()>=2)
353  {
354  auto s_start0 = *intervals.begin();
355  auto s_end1 = *intervals.rbegin();
356  // the first functions are likely to start with the reference line
357  // if no functions exist at x0, stretch all functions at fist interall point to x0
358  auto functions = getFunctionsAtParameter(x0);
359  if(functions.size()<1)
360  {
361  functions = getFunctionsAtParameter(s_start0);
362  for(auto f = functions.begin(); f!=functions.end(); f++)
363  {
364  auto fun = f->first;
365  fun->stretchDomain(x0,fun->limitHi());
366  }
367  intervals.insert(x0);
368  }
369  // the last functions are likely to end with the reference line
370  // if no functions exist at x1, stretch all functions at last interall point to x0
371  functions = getFunctionsAtParameter(x1);
372  if(functions.size()<1)
373  {
374  functions = getFunctionsAtParameter(s_end1);
375  for(auto f = functions.begin(); f!=functions.end(); f++)
376  {
377  auto fun = f->first;
378  fun->stretchDomain(fun->limitLo(),x1);
379  }
380  intervals.insert(x1);
381  }
382  }
383  return;
384 
385  // further ideas: multilane treatment
386  // combine close interval values at start and end
387  }
393  {
394  for(auto it = functions_.begin(); it!=functions_.end(); it++)
395  {
396  if(!((*it).first==nullptr))
397  {
398  delete (*it).first;
399  }
400  }
401  }
402  };
403 
414  bool oneway_;
416  std::set<double> intervals_;
417 
425  {
426  adoreMatrix<double,0,0> m;
427  m.set_size(3,geometry.size());
428  m(0,0) = 0.0;
429  m(1,0) = geometry.at(0).m_X;
430  m(2,0) = geometry.at(0).m_Y;
431  for(int i = 1; i<geometry.size(); i++)
432  {
433  m(0,i) = m(0,i-1)+geometry.at(i).distance(geometry.at(i-1));
434  m(1,i) = geometry.at(i).m_X;
435  m(2,i) = geometry.at(i).m_Y;
436  }
437  TGeometryFunction* fun = new TGeometryFunction(m);
438  LOG_T("limitLo: %.2f\tlimithi: %.2f", fun->limitLo(), fun->limitHi());
439  return fun;
440  }
441 
442  public:
444  {
445  referenceLine_ = nullptr;
446  oneway_ = false;
447  }
454  {
456  oneway_ = rl.oneway_;
457  }
464  {
465  if(referenceLine_ == nullptr) throw("Section::referenceLine_ is undefined.");
466  LOG_T("LaneBorder ID: %i", lb.id_);
468  // determine whether lane is left or right from center
469  double d = 0;
470  for(auto point = lb.geometry_.begin(); point!=lb.geometry_.end(); point++)
471  {
472  referenceLine_->getClosestParameter(point->m_X,point->m_Y,1,2,d);
473  if(abs(d) > 0.05) break;
474  }
475  auto p_start = fun->f(fun->limitLo());
476  auto p_end = fun->f(fun->limitHi());
477  double d_start, d_end;
478  double s_start = referenceLine_->getClosestParameter(p_start(0),p_start(1),1,2,d_start);
479  double s_end = referenceLine_->getClosestParameter(p_end(0), p_end(1), 1,2,d_end);
480  // invert if necessary
481  if(s_start < s_end)
482  {
483  fun->stretchDomain(s_start, s_end);
484  }
485  else
486  {
487  fun->invertDomain();
488  fun->stretchDomain(s_end,s_start);
489  }
490  if(d <= 0)
491  {
493  }
494  else
495  {
497  }
498  intervals_.insert(s_start);
499  intervals_.insert(s_end);
500  LOG_T("p_start: (%.2f|%.2f)",p_start(0),p_start(1));
501  LOG_T("p_end : (%.2f|%.2f)",p_end(0),p_end(1));
502  LOG_T("s_start: %.2f",s_start);
503  LOG_T("s_end : %.2f",s_end);
504  }
510  std::set<double> getIntervals()
511  {
512  return intervals_;
513  }
520  std::vector<TFunctionTypePair> getRightFunctionsAtParameter(double x)
521  {
523  }
530  std::vector<TFunctionTypePair> getLeftFunctionsAtParameter(double x)
531  {
533  }
540  {
541  return referenceLine_;
542  }
547  void cleanup()
548  {
549  delete referenceLine_;
552  }
557  void mend()
558  {
561  }
568  {
569  return functionMapRight_;
570  }
577  {
578  return functionMapLeft_;
579  }
580  bool isOneway()
581  {
582  return oneway_;
583  }
585  {
586  }
587  };
588  }
589 }
storage class to access different functions by their domain intervals
Definition: r2sauxiliary.h:250
std::vector< TFunctionTypePair > functions_
Definition: r2sauxiliary.h:252
void resetSParameter(TGeometryFunction *geometry)
Definition: r2sauxiliary.h:280
void addFunction(TGeometryFunction *function, LaneBorder::TYPE type)
add function to container
Definition: r2sauxiliary.h:298
std::vector< TFunctionTypePair > getFunctions()
getter method
Definition: r2sauxiliary.h:326
std::vector< TFunctionTypePair > getFunctionsStartingAtPoint(double x, double y)
Definition: r2sauxiliary.h:254
void mend(TGeometryFunction *refLine, std::set< double > &intervals)
try to repair some mistakes that might occur due to matching to closest point on refline
Definition: r2sauxiliary.h:336
std::vector< TFunctionTypePair > getFunctionsEndingAtPoint(double x, double y)
Definition: r2sauxiliary.h:267
std::vector< TFunctionTypePair > getFunctionsAtParameter(double x)
returns functions that are valid at the given parameter
Definition: r2sauxiliary.h:308
void deleteFunctions()
delete function objects
Definition: r2sauxiliary.h:392
directed borders, ordered from center line to outer border
Definition: r2sauxiliary.h:244
std::set< double > intervals_
Definition: r2sauxiliary.h:416
std::vector< TFunctionTypePair > getLeftFunctionsAtParameter(double x)
get functions that are valid at given parameter
Definition: r2sauxiliary.h:530
FunctionMap functionMapLeft_
storage for functions originally in the opposite direction as the reference line, stored in functions...
Definition: r2sauxiliary.h:413
FunctionMap functionMapRight_
storage for functions in the same directions as the reference line
Definition: r2sauxiliary.h:408
std::vector< TFunctionTypePair > getRightFunctionsAtParameter(double x)
get functions that are valid at given parameter
Definition: r2sauxiliary.h:520
TGeometryFunction * geometry2function(TR2SGeometry geometry)
easy conversion from a coordinate vector to a LLinearPiecewiseFunction
Definition: r2sauxiliary.h:424
void cleanup()
delete objects in pointers
Definition: r2sauxiliary.h:547
bool oneway_
Definition: r2sauxiliary.h:414
Section()
Definition: r2sauxiliary.h:443
FunctionMap getFunctionMapRight()
getter
Definition: r2sauxiliary.h:567
TGeometryFunction * getReferenceLineFunction()
getter
Definition: r2sauxiliary.h:539
void mend()
try to repair mistakes that occured during function generation
Definition: r2sauxiliary.h:557
std::set< double > getIntervals()
getter
Definition: r2sauxiliary.h:510
FunctionMap getFunctionMapLeft()
getter
Definition: r2sauxiliary.h:576
void addLaneBorder(LaneBorder lb)
add lane border to section, determine its valid range
Definition: r2sauxiliary.h:463
TGeometryFunction * referenceLine_
Definition: r2sauxiliary.h:415
void setReferenceLine(ReferenceLine rl)
setter
Definition: r2sauxiliary.h:453
~Section()
Definition: r2sauxiliary.h:584
bool isOneway()
Definition: r2sauxiliary.h:580
virtual CT f(DT x) const override
Definition: llinearpiecewisefunction.h:251
double getClosestParameter(T px, T py, int d1, int d2, T &n_min) const
Definition: llinearpiecewisefunction.h:1014
virtual void stretchDomain(DT x0, DT x1)
Definition: llinearpiecewisefunction.h:344
virtual void invertDomain()
Definition: llinearpiecewisefunction.h:320
virtual DT limitLo() const override
Definition: llinearpiecewisefunction.h:264
adoreMatrix< T, n+1, 0 > & getData()
Definition: llinearpiecewisefunction.h:147
virtual DT limitHi() const override
Definition: llinearpiecewisefunction.h:259
#define LOG_T(...)
log on trace level
Definition: csvlog.h:28
@ OTHER
Definition: border.h:38
@ DRIVING
Definition: border.h:39
std::vector< LaneBorder > TLaneBorderVector
Definition: r2sauxiliary.h:236
std::pair< TGeometryFunction *, LaneBorder::TYPE > TFunctionTypePair
Definition: r2sauxiliary.h:238
adore::mad::LLinearPiecewiseFunctionM< double, 2 > TGeometryFunction
Definition: r2sauxiliary.h:237
std::vector< ReferenceLine > TReferenceLineVector
Definition: r2sauxiliary.h:235
std::vector< adore::env::BorderBased::Coordinate > TR2SGeometry
Definition: r2sauxiliary.h:28
adore::mad::LLinearPiecewiseFunctionM< double, 3 > function_type_xyz
Definition: linearfunctiontypedefs.h:22
T min(T a, T b, T c, T d)
Definition: adoremath.h:663
x0
Definition: adore_set_goal.py:25
x
Definition: adore_set_goal.py:30
y
Definition: adore_set_goal.py:31
x1
Definition: adore_set_pose.py:28
Definition: areaofeffectconverter.h:20
This struct identifies a Border by the coordinates of the starting and the end point.
Definition: borderid.h:31
double distance(const BorderID &other) const
returns sum of distance between this.m_first and other.m_first and distance between this....
Definition: borderid.h:51
The border struct contains data of the smallest.
Definition: border.h:62
BorderType::TYPE m_type
Definition: border.h:71
BorderID * m_left
Definition: border.h:69
This struct represents 3-dimensional coordines.
Definition: coordinate.h:34
basic storage struct for lane borders from file
Definition: r2sauxiliary.h:143
adore::env::BorderBased::BorderID getBorderID() const
Definition: r2sauxiliary.h:169
TYPE type_
Definition: r2sauxiliary.h:158
TYPE
Definition: r2sauxiliary.h:145
@ DRIVING
Definition: r2sauxiliary.h:147
@ TOWN
Definition: r2sauxiliary.h:152
@ NONE
Definition: r2sauxiliary.h:146
@ SHOULDER
Definition: r2sauxiliary.h:150
@ PARKING
Definition: r2sauxiliary.h:149
@ BIKE
Definition: r2sauxiliary.h:148
@ RESTRICTED
Definition: r2sauxiliary.h:151
@ OTHER
Definition: r2sauxiliary.h:153
int parent_id_
Definition: r2sauxiliary.h:156
adore::env::BorderBased::Border * getBorder(const adore::env::BorderBased::BorderID &leftID) const
Definition: r2sauxiliary.h:222
double distance(const ReferenceLine &rl) const
Definition: r2sauxiliary.h:190
bool isOnRightHandSide(const ReferenceLine &rl) const
Definition: r2sauxiliary.h:173
int id_
Definition: r2sauxiliary.h:155
adore::mad::function_type_xyz * getBorderPath() const
Definition: r2sauxiliary.h:195
bool isSane() const
Definition: r2sauxiliary.h:165
double sortingDistance(const ReferenceLine &rl) const
Definition: r2sauxiliary.h:178
LaneBorder()
Definition: r2sauxiliary.h:159
TR2SGeometry geometry_
Definition: r2sauxiliary.h:157
basic storage struct for ReferenceLine from file
Definition: r2sauxiliary.h:35
adore::env::BorderBased::Border * getBorder() const
Definition: r2sauxiliary.h:119
adore::env::BorderBased::BorderID getInverseBorderID() const
Definition: r2sauxiliary.h:61
adore::mad::function_type_xyz * getInverseBorderPath() const
Definition: r2sauxiliary.h:92
int id_
Definition: r2sauxiliary.h:41
LINETYPE
Definition: r2sauxiliary.h:37
@ StandardLine
Definition: r2sauxiliary.h:38
@ ConnectionLine
Definition: r2sauxiliary.h:39
adore::mad::function_type_xyz * getBorderPath() const
Definition: r2sauxiliary.h:65
LINETYPE linetype_
Definition: r2sauxiliary.h:44
bool isSane() const
Definition: r2sauxiliary.h:52
bool oneway_
Definition: r2sauxiliary.h:43
ReferenceLine()
Definition: r2sauxiliary.h:46
TR2SGeometry geometry_
Definition: r2sauxiliary.h:42
adore::env::BorderBased::BorderID getBorderID() const
Definition: r2sauxiliary.h:57
adore::env::BorderBased::Border * getInverseBorder() const
Definition: r2sauxiliary.h:128