ilqgames
A new real-time solver for large-scale differential games.
roundabout_lane_center.cpp
1 /*
2  * Copyright (c) 2019, The Regents of the University of California (Regents).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: David Fridovich-Keil ( dfk@eecs.berkeley.edu )
35  */
36 
37 ///////////////////////////////////////////////////////////////////////////////
38 //
39 // Compute a lane center entering and leaving a roundabout.
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 #include <ilqgames/utils/types.h>
44 
45 #include <glog/logging.h>
46 #include <vector>
47 
48 namespace ilqgames {
49 
50 PointList2 RoundaboutLaneCenter(float entrance_angle, float exit_angle,
51  float distance_from_roundabout) {
52  // Radius of roundabout and lane half width.
53  constexpr float kRoundaboutRadius = 12.0; // m
54  constexpr float kLaneHalfWidth = 2.5; // m
55 
56  // Beginning of small 90 degree arc onto roundabout.
57  const Point2 entry_arc_center(
58  (kRoundaboutRadius + kLaneHalfWidth) * std::cos(entrance_angle),
59  (kRoundaboutRadius + kLaneHalfWidth) * std::sin(entrance_angle));
60 
61  // Initial point in lane.
62  const float first_entry_arc_point_angle = entrance_angle - M_PI_2;
63  const Point2 first_point_in_entry_arc =
64  entry_arc_center +
65  kLaneHalfWidth * Point2(std::cos(first_entry_arc_point_angle),
66  std::sin(first_entry_arc_point_angle));
67 
68  PointList2 points = {
69  first_point_in_entry_arc +
70  distance_from_roundabout *
71  Point2(std::cos(entrance_angle), std::sin(entrance_angle)),
72  first_point_in_entry_arc};
73 
74  // Insert a short arc to take us into the roundabout.
75  constexpr size_t kNumPointsInArc = 3;
76  for (size_t ii = 1; ii <= kNumPointsInArc; ii++) {
77  const float angle = first_entry_arc_point_angle -
78  M_PI_2 * static_cast<float>(ii) / kNumPointsInArc;
79  points.push_back(entry_arc_center +
80  kLaneHalfWidth * Point2(std::cos(angle), std::sin(angle)));
81  }
82 
83  // We should be back to the first point on the roundabout.
84  const Point2 first_point_on_roundabout(
85  kRoundaboutRadius * std::cos(entrance_angle),
86  kRoundaboutRadius * std::sin(entrance_angle));
87  CHECK_LT((points.back() - first_point_on_roundabout).norm(),
88  constants::kSmallNumber);
89 
90  // Rest of the points in the roundabout.
91  constexpr size_t kNumPointsInRoundabout = 10;
92  for (size_t ii = 1; ii <= kNumPointsInRoundabout; ii++) {
93  const float next_angle = entrance_angle + (exit_angle - entrance_angle) *
94  static_cast<float>(ii) /
95  kNumPointsInRoundabout;
96  points.emplace_back(kRoundaboutRadius * std::cos(next_angle),
97  kRoundaboutRadius * std::sin(next_angle));
98  }
99 
100  // Final point.
101  constexpr float kFarAway = 1e4; // m
102  points.emplace_back(kFarAway * std::cos(exit_angle),
103  kFarAway * std::sin(exit_angle));
104 
105  return points;
106 }
107 
108 } // namespace ilqgames