opendrive_lane.cpp 22 KB
Newer Older
1
#include "opendrive_lane.h"
2
#include "exceptions.h"
3
#include <math.h>
4
5
6
7

COpendriveLane::COpendriveLane()
{
  this->nodes.clear();
8
9
  this->next.clear();
  this->prev.clear();
10
11
12
13
  this->left_lane=NULL;
  this->left_mark=OD_MARK_NONE;
  this->right_lane=NULL;
  this->right_mark=OD_MARK_NONE;
14
  this->segment=NULL;
15
  this->resolution=DEFAULT_RESOLUTION;
16
17
18
19
  this->scale_factor=DEFAULT_SCALE_FACTOR;
  this->width=0.0;
  this->speed=0.0;
  this->offset=0.0;
20
  this->id=0;
21
  this->index=-1;
22
23
}

24
COpendriveLane::COpendriveLane(const COpendriveLane &object)
25
{
26
27
28
  this->nodes=object.nodes;
  this->next=object.next;
  this->prev=object.prev;
29
30
31
32
  this->left_lane=object.left_lane;
  this->left_mark=object.left_mark;
  this->right_lane=object.right_lane;
  this->right_mark=object.right_mark;
33
  this->segment=object.segment;
34
35
  this->resolution=object.resolution;
  this->scale_factor=object.scale_factor;
36
37
38
  this->width=object.width;
  this->speed=object.speed;
  this->offset=object.offset;
39
  this->id=object.id;
40
  this->index=object.index;
41
42
}

43
void COpendriveLane::link_neightbour_lane(COpendriveLane *lane)
44
{
Sergi Hernandez's avatar
Sergi Hernandez committed
45
46
  unsigned int min_num_nodes;

47
48
  if(lane!=NULL)
  {
Sergi Hernandez's avatar
Sergi Hernandez committed
49
50
51
52
53
    if(this->get_num_nodes()<lane->get_num_nodes())
      min_num_nodes=this->get_num_nodes();
    else
      min_num_nodes=lane->get_num_nodes();
    for(unsigned int i=0;i<min_num_nodes-1;i++)
54
    {
55
      this->nodes[i]->add_link(lane->nodes[i+1],this->left_mark,this->segment,this);
Sergi Hernandez's avatar
Sergi Hernandez committed
56
      lane->nodes[i]->add_link(this->nodes[i+1],this->left_mark,this->segment,lane);
57
58
59
60
61
      if(this->id>0)// left lanes
        this->right_mark=lane->left_mark;
      else// right lanes
        this->left_mark=lane->right_mark;
    } 
Sergi Hernandez's avatar
Sergi Hernandez committed
62
63
64
65
66
    if(min_num_nodes>0)
    {
      this->left_lane=lane;
      lane->right_lane=this;
    }
67
68
69
  }
}

70
void COpendriveLane::link_lane(COpendriveLane *lane,opendrive_mark_t mark,bool from_start, bool to_start)
71
{
72
  COpendriveRoadNode *start,*end;
Sergi Hernandez's avatar
Sergi Hernandez committed
73
  std::stringstream error;
74

75
76
  if(lane!=NULL)
  {
77
    if(this->get_num_nodes()>0 && lane->get_num_nodes()>0)
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    {
      if(from_start)
      {
        if(this->id<0)
          start=this->nodes[0];
        else
          start=this->nodes[this->nodes.size()-1];
        if(to_start)
        {
          if(lane->id<0)
            end=lane->nodes[0];
          else
            end=lane->nodes[lane->nodes.size()-1];
        }
        else
        {
          if(lane->id<0)
            end=lane->nodes[lane->nodes.size()-1];
          else
            end=lane->nodes[0];
        }
      }
      else
      {
        if(this->id<0)
          start=this->nodes[this->nodes.size()-1];
        else
          start=this->nodes[0];
        if(to_start)
        {
          if(lane->id<0)
            end=lane->nodes[0];
          else
            end=lane->nodes[lane->nodes.size()-1];
        }
        else
        {
          if(lane->id<0)
            end=lane->nodes[lane->nodes.size()-1];
          else
            end=lane->nodes[0];
        }
      }
121
122
123
124
      start->add_link(end,mark,this->segment,this);
      // link lane
      this->add_next_lane(lane);
      lane->add_prev_lane(this);
125
    }
126
    else 
Sergi Hernandez's avatar
Sergi Hernandez committed
127
128
129
130
    {
      error << "Lane " << this->id << " of segment " << this->segment->get_name() << " or lane " << lane->get_id() << " of segment " << lane->get_segment().get_name() << " has no nodes";
      throw CException(_HERE_,error.str());
    }
131
132
133
  }
}

134
void COpendriveLane::add_next_lane(COpendriveLane *lane)
135
{
136
137
  for(unsigned int i=0;i<this->next.size();i++)
    if(this->next[i]==lane)// lane is already linked
138
      return;
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

  // add the lane
  this->next.push_back(lane);
}

void COpendriveLane::add_prev_lane(COpendriveLane *lane)
{
  for(unsigned int i=0;i<this->prev.size();i++)
    if(this->prev[i]==lane)// lane is already linked
      return;

  // add the lane
  this->prev.push_back(lane);
}

Sergi Hernandez's avatar
Sergi Hernandez committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
void COpendriveLane::remove_lane(COpendriveLane *lane)
{
  std::vector<COpendriveLane *>::iterator lane_it;

  // remove the reference from neightbour lanes
  if(this->right_lane==lane)
    this->right_lane=NULL;
  if(this->left_lane==lane)
    this->left_lane=NULL;
  // remove the reference from next lanes
  for(lane_it=this->next.begin();lane_it!=this->next.end();)
  {
    if((*lane_it)==lane)
      lane_it=this->next.erase(lane_it);
     else
      lane_it++;
  }
  // remove the reference from next lanes
  for(lane_it=this->prev.begin();lane_it!=this->prev.end();)
  {
    if((*lane_it)==lane)
      lane_it=this->prev.erase(lane_it);
     else
      lane_it++;
  }
}

181
182
183
184
void COpendriveLane::add_node(COpendriveRoadNode *node)
{
  if(this->has_node(node))
    return;
Sergi Hernandez's avatar
Sergi Hernandez committed
185
186
  // link the new node with the previous one in the current lane, if any
  if(this->nodes.size()>0)
187
    this->nodes[this->nodes.size()-1]->add_link(node,OD_MARK_NONE,this->segment,this);
188
189
  // add the new node
  this->nodes.push_back(node);
190
191
}

192
193
194
195
196
197
198
199
200
bool COpendriveLane::has_node(COpendriveRoadNode *node)
{
  for(unsigned int i=0;i<this->nodes.size();i++)
    if(this->nodes[i]==node)
      return true;

  return false;
}

Sergi Hernandez's avatar
Sergi Hernandez committed
201
bool COpendriveLane::has_node_with_index(unsigned int index)
202
203
204
205
206
207
208
209
210
211
212
213
214
{
  for(unsigned int i=0;i<this->nodes.size();i++)
    if(this->nodes[i]->get_index()==index)
      return true;

  return false;
}

void COpendriveLane::set_parent_segment(COpendriveRoadSegment *segment)
{
  this->segment=segment;
}

Sergi Hernandez's avatar
Sergi Hernandez committed
215
216
217
218
219
220
221
222
223
224
225
226
void COpendriveLane::set_left_lane(COpendriveLane *lane,opendrive_mark_t mark)
{
  this->left_lane=lane;
  this->left_mark=mark;
}

void COpendriveLane::set_right_lane(COpendriveLane *lane,opendrive_mark_t mark)
{
  this->right_lane=lane;
  this->right_mark=mark;
}

227
void COpendriveLane::set_resolution(double res)
228
229
230
{
  this->resolution=res;

231
232
233
234
235
236
237
238
239
240
241
242
  for(unsigned int i=0;i<this->nodes.size();i++)
    this->nodes[i]->set_resolution(res);
}

void COpendriveLane::set_scale_factor(double scale)
{ 
  this->scale_factor=scale;

  for(unsigned int i=0;i<this->nodes.size();i++)
    this->nodes[i]->set_scale_factor(scale);
}

243
244
245
246
247
248
249
250
251
252
void COpendriveLane::set_width(double width)
{
  this->width=width;
}

void COpendriveLane::set_speed(double speed)
{
  this->speed=speed;
}

253
254
255
256
257
void COpendriveLane::set_offset(double offset)
{
  this->offset=offset;
}

258
void COpendriveLane::set_id(int id)
259
{
260
261
  this->id=id;
}
262

263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
void COpendriveLane::set_index(unsigned int index)
{
  this->index=index;
}

bool COpendriveLane::updated(lane_up_ref_t &refs)
{
  lane_up_ref_t::iterator updated_it;

  for(updated_it=refs.begin();updated_it!=refs.end();updated_it++)
    if(updated_it->second==this)
      return true;

  return false;
}

void COpendriveLane::update_references(segment_up_ref_t &segment_refs,lane_up_ref_t &lane_refs,node_up_ref_t &node_refs)
{
  std::vector<COpendriveLane *>::iterator lane_it;
  std::vector<COpendriveRoadNode *>::iterator node_it;
 
  if(this->updated(lane_refs))
  {
    for(node_it=this->nodes.begin();node_it!=this->nodes.end();node_it++)
    {
      if(node_refs.find(*node_it)!=node_refs.end())
      {
        (*node_it)=node_refs[*node_it];
        (*node_it)->update_references(segment_refs,lane_refs,node_refs);
      }
      else if((*node_it)->updated(node_refs))
        (*node_it)->update_references(segment_refs,lane_refs,node_refs);
    }
    for(lane_it=this->next.begin();lane_it!=this->next.end();lane_it++)
      if(lane_refs.find(*lane_it)!=lane_refs.end())
        (*lane_it)=lane_refs[*lane_it];
    for(lane_it=this->prev.begin();lane_it!=this->prev.end();lane_it++)
      if(lane_refs.find(*lane_it)!=lane_refs.end())
        (*lane_it)=lane_refs[*lane_it];
    if(lane_refs.find(this->left_lane)!=lane_refs.end())
      this->left_lane=lane_refs[this->left_lane];
    if(lane_refs.find(this->right_lane)!=lane_refs.end())
      this->right_lane=lane_refs[this->right_lane];
    if(segment_refs.find(this->segment)!=segment_refs.end())
      this->segment=segment_refs[this->segment];
  }
}

void COpendriveLane::clean_references(segment_up_ref_t &segment_refs,lane_up_ref_t &lane_refs,node_up_ref_t &node_refs)
{
  std::vector<COpendriveLane *>::iterator lane_it;
  std::vector<COpendriveRoadNode *>::iterator node_it;

  if(this->updated(lane_refs))
  {
    for(node_it=this->nodes.begin();node_it!=this->nodes.end();)
    {
Sergi Hernandez's avatar
Sergi Hernandez committed
320
      if((*node_it)->updated(node_refs))
321
322
323
324
325
326
327
328
329
      {
        (*node_it)->clean_references(segment_refs,lane_refs,node_refs);
        node_it++;
      }
      else
        node_it=this->nodes.erase(node_it);
    }
    for(lane_it=this->next.begin();lane_it!=this->next.end();)
    {
Sergi Hernandez's avatar
Sergi Hernandez committed
330
      if(!(*lane_it)->updated(lane_refs))
331
332
333
334
335
336
        lane_it=this->next.erase(lane_it);
      else
        lane_it++;
    }
    for(lane_it=this->prev.begin();lane_it!=this->prev.end();)
    {
Sergi Hernandez's avatar
Sergi Hernandez committed
337
      if(!(*lane_it)->updated(lane_refs))
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
        lane_it=this->prev.erase(lane_it);
      else
        lane_it++;
    }
    if(lane_refs.find(this->left_lane)!=lane_refs.end())
      this->left_lane=lane_refs[this->left_lane];
    else if(!this->left_lane->updated(lane_refs))
      this->left_lane=NULL;
    if(lane_refs.find(this->right_lane)!=lane_refs.end())
      this->right_lane=lane_refs[this->right_lane];
    else if(!this->right_lane->updated(lane_refs))
      this->right_lane=NULL;
  }
}

353
void COpendriveLane::update_start_node(node_up_ref_t &new_node_ref,lane_up_ref_t &new_lane_ref,TOpendriveWorldPose *start)
354
355
356
357
358
{
  std::vector<COpendriveRoadNode *>::iterator it;
  segment_up_ref_t new_segment_ref;
  unsigned int start_node_index,i;
  COpendriveRoadNode *new_node;
Sergi Hernandez's avatar
Sergi Hernandez committed
359
  std::stringstream error;
360
361
362
  
  if(start==NULL)
    return;
Sergi Hernandez's avatar
Sergi Hernandez committed
363
364
365
366
367
368
  start_node_index=this->get_closest_node_index(*start,3.14159);
  if(start_node_index==(unsigned int)-1)
  {
    error << "No node close to (x=" << start->x << ",y=" << start->y << ",heading=" << start->heading << ") in lane " << this->id << " of segment " << this->segment->get_name();
    throw CException(_HERE_,error.str());
  }
369
370
371
372
  // eliminate all the node before the start one
  for(it=this->nodes.begin(),i=0;it!=this->nodes.end();i++)
  {
    if(i<start_node_index)
373
    {
Sergi Hernandez's avatar
Sergi Hernandez committed
374
375
376
377
378
      if((*it)->updated(new_node_ref))
      {
        new_node_ref.erase((*it)->get_original_node(new_node_ref));
        delete *it;
      }
379
      it=this->nodes.erase(it);
380
    }
381
382
    else
    {
Sergi Hernandez's avatar
Sergi Hernandez committed
383
      if(!(*it)->updated(new_node_ref))
384
385
386
387
388
389
390
391
392
393
394
395
      {
        new_node=new COpendriveRoadNode(**it);
        new_node_ref[*it]=new_node;
      }
      it++;
    }
  }
  this->prev.clear();// it is no longer connected to any previous lane
  this->update_references(new_segment_ref,new_lane_ref,new_node_ref);
  this->nodes[0]->update_start_pose(this,start);
}

396
void COpendriveLane::update_end_node(node_up_ref_t &new_node_ref,lane_up_ref_t &new_lane_ref,TOpendriveWorldPose *end)
397
398
399
400
401
{
  std::vector<COpendriveRoadNode *>::iterator it;
  segment_up_ref_t new_segment_ref;
  unsigned int end_node_index,i;
  COpendriveRoadNode *new_node;
Sergi Hernandez's avatar
Sergi Hernandez committed
402
  std::stringstream error;
403
404
    
  if(end==NULL)
405
    return;
Sergi Hernandez's avatar
Sergi Hernandez committed
406
407
408
409
410
411
  end_node_index=this->get_closest_node_index(*end,3.14159);
  if(end_node_index==(unsigned int)-1)
  {
    error << "No node close to (x=" << end->x << ",y=" << end->y << ",heading=" << end->heading << ") in lane " << this->id << " of segment " << this->segment->get_name();
    throw CException(_HERE_,error.str());
  }
412
413
414
415
  // eliminate all the node before the start one
  for(it=this->nodes.begin(),i=0;it!=this->nodes.end();i++)
  {
    if(i>end_node_index)
416
    {
Sergi Hernandez's avatar
Sergi Hernandez committed
417
418
419
420
421
      if((*it)->updated(new_node_ref))
      {
        new_node_ref.erase((*it)->get_original_node(new_node_ref));
        delete *it;
      }
422
      it=this->nodes.erase(it);
423
    }
424
425
    else
    {
Sergi Hernandez's avatar
Sergi Hernandez committed
426
      if(!(*it)->updated(new_node_ref))
427
428
429
430
431
432
433
434
435
      {
        new_node=new COpendriveRoadNode(**it);
        new_node_ref[*it]=new_node;
      }
      it++;
    }
  }
  this->next.clear();// it is no longer connected to any next lane
  this->update_references(new_segment_ref,new_lane_ref,new_node_ref);
436
  this->nodes[this->nodes.size()-1]->update_end_pose(this,end);
437
438
}

439
COpendriveLane *COpendriveLane::get_sub_lane(node_up_ref_t &new_node_ref,lane_up_ref_t &new_lane_ref,TOpendriveWorldPose *start,TOpendriveWorldPose *end)
440
441
442
443
444
445
446
447
448
449
{
  COpendriveLane *new_lane;

  if(start==NULL && end==NULL)
    return this->clone(new_node_ref,new_lane_ref);
  new_lane=new COpendriveLane(*this);
  new_lane_ref[this]=new_lane;
  if(this->id<0)
  {
    new_lane->update_start_node(new_node_ref,new_lane_ref,start);
450
    new_lane->update_end_node(new_node_ref,new_lane_ref,end);
451
452
453
454
  }
  else
  {
    new_lane->update_start_node(new_node_ref,new_lane_ref,end);
455
    new_lane->update_end_node(new_node_ref,new_lane_ref,start);
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
  }

  return new_lane;
}

COpendriveLane *COpendriveLane::clone(node_up_ref_t &new_node_ref,lane_up_ref_t &new_lane_ref)
{
  COpendriveLane *new_lane;
  std::vector<COpendriveRoadNode *>::iterator it;
  COpendriveRoadNode *new_node;
  segment_up_ref_t new_segment_ref;

  new_lane=new COpendriveLane(*this);
  new_lane_ref[this]=new_lane;
  for(it=new_lane->nodes.begin();it!=new_lane->nodes.end();it++)
  {
    new_node=new COpendriveRoadNode(**it);
    new_node_ref[*it]=new_node;
  }
  this->update_references(new_segment_ref,new_lane_ref,new_node_ref);

  return new_lane;
478
479
}

Sergi Hernandez's avatar
Sergi Hernandez committed
480
void COpendriveLane::load(const ::lane &lane_info,COpendriveRoadSegment *segment)
481
482
{
  std::stringstream error;
483
  opendrive_mark_t mark;
484
485

  this->nodes.clear();
Sergi Hernandez's avatar
Sergi Hernandez committed
486
487
  this->prev.clear();
  this->next.clear();
488
489
490
491
  this->left_lane=NULL;
  this->left_mark=OD_MARK_NONE;
  this->right_lane=NULL;
  this->right_mark=OD_MARK_NONE;
492
  this->set_id(lane_info.id().get());
493
  // import lane width
Sergi Hernandez's avatar
Sergi Hernandez committed
494
495
496
497
498
499
  if(lane_info.width().size()<1)
  {
    error << "Lane " << this->id << " of segment " << segment->get_name() << " has no width record";
    throw CException(_HERE_,error.str());
  }
  else if(lane_info.width().size()>1)
500
  {
Sergi Hernandez's avatar
Sergi Hernandez committed
501
    error << "Lane " << this->id << " of segment " << segment->get_name() << " has more than one width record";
502
503
    throw CException(_HERE_,error.str());
  }
504
  this->set_width(lane_info.width().begin()->a().get());
505
  // import lane speed
Sergi Hernandez's avatar
Sergi Hernandez committed
506
  if(lane_info.speed().size()<1)
507
  {
Sergi Hernandez's avatar
Sergi Hernandez committed
508
509
510
511
512
513
    error << "Lane " << this->id << " of segment " << segment->get_name() << " has no speed record";
    throw CException(_HERE_,error.str());
  }
  else if(lane_info.speed().size()>1)
  {
    error << "Lane " << this->id << " of segment " << segment->get_name() << " has more than one speed record";
514
515
    throw CException(_HERE_,error.str());
  }
516
  this->set_speed(lane_info.speed().begin()->max().get());
517
  //lane mark
518
519
520
  if(lane_info.roadMark().size()==0)
    mark=OD_MARK_NONE;
  else if(lane_info.roadMark().size()>1)
521
  {
Sergi Hernandez's avatar
Sergi Hernandez committed
522
    error << "Lane " << this->id << " of segment " << segment->get_name() << " has more than one road mark record";
523
524
    throw CException(_HERE_,error.str());
  }
525
  else if(lane_info.roadMark().size()==1)
526
  {
527
528
529
530
531
532
533
534
    if(lane_info.roadMark().begin()->type1().present())
    {
      if(lane_info.roadMark().begin()->type1().get()=="none")
        mark=OD_MARK_NONE;
      else if(lane_info.roadMark().begin()->type1().get()=="solid")
        mark=OD_MARK_SOLID;
      else if(lane_info.roadMark().begin()->type1().get()=="broken")
        mark=OD_MARK_BROKEN;
535
      else if(lane_info.roadMark().begin()->type1().get()=="solid solid")
536
        mark=OD_MARK_SOLID_SOLID;
537
      else if(lane_info.roadMark().begin()->type1().get()=="solid broken")
538
        mark=OD_MARK_SOLID_BROKEN;
539
      else if(lane_info.roadMark().begin()->type1().get()=="broken solid")
540
        mark=OD_MARK_BROKEN_SOLID;
541
      else if(lane_info.roadMark().begin()->type1().get()=="broken broken")
542
543
544
545
        mark=OD_MARK_BROKEN_BROKEN;
      else
        mark=OD_MARK_NONE;
    }
546
547
548
549
550
551
552
    else
      mark=OD_MARK_NONE;
  }
  if(this->id<0)//right lanes
    this->right_mark=mark;
  else
    this->left_mark=mark;
553

554
  this->set_parent_segment(segment);
555
556
}

557
unsigned int COpendriveLane::get_num_nodes(void) const
558
559
560
561
{
  return this->nodes.size();
}

562
const COpendriveRoadNode &COpendriveLane::get_node(unsigned int index) const
563
{
Sergi Hernandez's avatar
Sergi Hernandez committed
564
565
  std::stringstream error;

566
567
568
  if(index>=0 && index<this->nodes.size())
    return *this->nodes[index];
  else
Sergi Hernandez's avatar
Sergi Hernandez committed
569
570
571
572
  {
    error << "Invalid node index (" << index << ") for segment " << this->segment->get_name() << " (lane " << this->id << ")"; 
    throw CException(_HERE_,error.str());
  }
573
574
}

575
576
577
578
579
580
581
unsigned int COpendriveLane::get_num_next_lanes(void) const
{
  return this->next.size();
}

const COpendriveLane &COpendriveLane::get_next_lane(unsigned int index) const
{
Sergi Hernandez's avatar
Sergi Hernandez committed
582
583
  std::stringstream error;

584
585
586
  if(index>=0 && index<this->next.size())
    return *this->next[index];
  else
Sergi Hernandez's avatar
Sergi Hernandez committed
587
588
589
590
  {
    error << "Invalid next lane index (" << index << ") for segment " << this->segment->get_name() << " (lane " << this->id << ")"; 
    throw CException(_HERE_,error.str());
  }
591
592
593
594
595
596
597
598
599
}

unsigned int COpendriveLane::get_num_prev_lanes(void) const
{
  return this->prev.size();
}

const COpendriveLane &COpendriveLane::get_prev_lane(unsigned int index) const
{
Sergi Hernandez's avatar
Sergi Hernandez committed
600
601
  std::stringstream error;

602
603
604
  if(index>=0 && index<this->prev.size())
    return *this->prev[index];
  else
Sergi Hernandez's avatar
Sergi Hernandez committed
605
606
607
608
  {
    error << "Invalid previous lane index (" << index << ") for segment " << this->segment->get_name() << " (lane " << this->id << ")"; 
    throw CException(_HERE_,error.str());
  }
609
610
}

611
const COpendriveRoadSegment &COpendriveLane::get_segment(void) const
612
{
613
  return *this->segment;
614
615
}

616
double COpendriveLane::get_width(void) const
617
{
618
  return this->width/this->scale_factor;
619
620
}

621
double COpendriveLane::get_speed(void) const
622
623
624
625
{
  return this->speed;
}

626
627
628
629
630
double COpendriveLane::get_offset(void) const
{
  return this->offset/this->scale_factor;
}

631
632
633
634
635
636
637
638
double COpendriveLane::get_center_offset(void) const
{
  if(this->id<0)
    return (this->offset-this->width/2.0)/this->scale_factor;
  else
    return (this->offset+this->width/2.0)/this->scale_factor;
}

639
640
641
642
643
unsigned int COpendriveLane::get_index(void) const
{
  return this->index;
}

644
int COpendriveLane::get_id(void) const
645
{
646
  return this->id;
647
648
}

649
TOpendriveWorldPose COpendriveLane::get_start_pose(void) const
650
{
651
652
  TOpendriveTrackPose track_pose;
  TOpendriveWorldPose world_pose;
Sergi Hernandez's avatar
Sergi Hernandez committed
653
  std::stringstream error;
654

655
  track_pose.t=0.0;
656
657
  if(this->id<0)// right lane
  {
658
    track_pose.s=0.0;
659
    track_pose.heading=0.0;
660
661
662
  }
  else
  {
663
664
    track_pose.s=this->segment->get_length();
    track_pose.heading=3.14159;
665
  }
666
  world_pose=this->transform_to_world_pose(track_pose);
667

668
  return world_pose;
669
670
}

671
TOpendriveWorldPose COpendriveLane::get_end_pose(void) const
672
{
673
674
  TOpendriveTrackPose track_pose;
  TOpendriveWorldPose world_pose;
Sergi Hernandez's avatar
Sergi Hernandez committed
675
  std::stringstream error;
676
  
677
678
679
680
681
  track_pose.t=0.0;
  if(this->id<0)// right_lane
  {
    track_pose.s=this->segment->get_length();
    track_pose.heading=0.0;
682
683
  }
  else
684
685
686
  {
    track_pose.s=0.0;
    track_pose.heading=3.14159;
687
  }
688
  world_pose=this->transform_to_world_pose(track_pose);
689

690
  return world_pose;
691
692
}

693
double COpendriveLane::get_length(void) const
694
{
Sergi Hernandez's avatar
Sergi Hernandez committed
695
  std::stringstream error;
696
697
698
699
  double length=0.0;

  for(unsigned int i=0;i<this->nodes.size();i++)
  {
700
    for(unsigned int j=0;j<this->nodes[i]->get_num_links();j++)
701
    {
702
703
      if(&this->nodes[i]->get_link(j).get_parent_lane()==this)
        length+=this->nodes[i]->links[j]->get_length();
704
705
706
707
708
709
    }
  }

  return length;
}

710
TOpendriveLocalPose COpendriveLane::transform_to_local_pose(TOpendriveTrackPose &track) const
711
{
712
  TOpendriveTrackPose pose;
713

714
715
716
  pose=track;
  pose.t+=this->get_center_offset();
  return this->segment->transform_to_local_pose(pose);
717
718
}

719
TOpendriveWorldPose COpendriveLane::transform_to_world_pose(TOpendriveTrackPose &track) const
720
{ 
721
  TOpendriveTrackPose pose;
722

723
724
725
  pose=track;
  pose.t+=this->get_center_offset();
  return this->segment->transform_to_world_pose(pose);
726
727
}

728
unsigned int COpendriveLane::get_closest_node_index(TOpendriveWorldPose &pose,double angle_tol)
729
730
{
  double dist,min_dist=std::numeric_limits<double>::max();
731
  TOpendriveWorldPose found_pose;
Sergi Hernandez's avatar
Sergi Hernandez committed
732
  unsigned int closest_index=-1;
733
734
735

  for(unsigned int i=0;i<this->nodes.size();i++)
  {
736
737
    this->nodes[i]->get_closest_pose(pose,found_pose,angle_tol);
    dist=sqrt(pow(found_pose.x-pose.x,2.0)+pow(found_pose.y-pose.y,2.0));
738
739
740
741
742
743
744
745
746
747
    if(dist<min_dist)
    {
      min_dist=dist;
      closest_index=i;
    }
  }

  return closest_index;
}

748
std::ostream& operator<<(std::ostream& out, COpendriveLane &lane)
749
{
750
  out << "  Lane: " << lane.id << " (offset: " << lane.offset << ")" << std::endl;
751
752
753
754
755
756
  out << "    width: " << lane.get_width() << std::endl;
  out << "    speed: " << lane.get_speed() << std::endl;
  if(lane.segment!=NULL)
    out << "    Parent road segment: " << lane.segment->get_name() << std::endl;
  else 
    out << "    No parent segment" << std::endl;
757
758
  if(lane.left_lane==NULL)
    out << "    No left lane (";
759
  else
760
    out << "    Left lane " << lane.left_lane->get_id() << " (";
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
  switch(lane.left_mark)
  {
    case OD_MARK_NONE:
      out << "no mark)" << std::endl;
      break;
    case OD_MARK_SOLID:
      out << "solid)" << std::endl;
      break;
    case OD_MARK_BROKEN:
      out << "broken)" << std::endl;
      break;
    case OD_MARK_SOLID_SOLID:
      out << "solid solid)" << std::endl;
      break;
    case OD_MARK_SOLID_BROKEN:
      out << "solid broken)" << std::endl;
      break;
    case OD_MARK_BROKEN_SOLID:
      out << "broken solid)" << std::endl;
      break;
    case OD_MARK_BROKEN_BROKEN:
      out << "broken broken)" << std::endl;
      break;
  }
785
786
  if(lane.right_lane==NULL)
    out << "    No right lane (";
787
  else
788
    out << "    Right lane " << lane.right_lane->get_id() << " (";
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
  switch(lane.right_mark)
  {
    case OD_MARK_NONE:
      out << "no mark)" << std::endl;
      break;
    case OD_MARK_SOLID:
      out << "solid)" << std::endl;
      break;
    case OD_MARK_BROKEN:
      out << "broken)" << std::endl;
      break;
    case OD_MARK_SOLID_SOLID:
      out << "solid solid)" << std::endl;
      break;
    case OD_MARK_SOLID_BROKEN:
      out << "solid broken)" << std::endl;
      break;
    case OD_MARK_BROKEN_SOLID:
      out << "broken solid)" << std::endl;
      break;
    case OD_MARK_BROKEN_BROKEN:
      out << "broken broken)" << std::endl;
      break;
  }
813
814
  out << "    Number of nodes: " << lane.nodes.size() << std::endl;
  for(unsigned int i=0;i<lane.nodes.size();i++)
815
    out << *lane.nodes[i];
816

817
  return out;
818
819
820
821
}

COpendriveLane::~COpendriveLane()
{
822
  this->nodes.clear();
823
824
  this->prev.clear();
  this->next.clear();
825
826
827
828
  this->left_lane=NULL;
  this->left_mark=OD_MARK_NONE;
  this->right_lane=NULL;
  this->right_mark=OD_MARK_NONE;
829
830
831
832
833
834
  this->segment=NULL;
  this->resolution=DEFAULT_RESOLUTION;
  this->scale_factor=DEFAULT_SCALE_FACTOR;
  this->width=0.0;
  this->speed=0.0;
  this->offset=0.0;
Sergi Hernandez's avatar
Sergi Hernandez committed
835
  this->id=0;
836
  this->index=-1;
837
}