Drake
connection.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 
6 #include "ignition/math/Vector3.hh"
7 #include "ignition/rndf/UniqueId.hh"
8 
12 
13 namespace drake {
14 namespace maliput {
15 namespace rndf {
16 
17 /// A container that holds the information needed by a Builder to construct one
18 /// or more Lane objects. This class will be created from a vector of
19 /// DirectedWaypoint objects (based on RNDF waypoints that belong to the same
20 /// RNDF lane).
21 ///
22 /// A Connection object derives one or multiple Lane objects. Consequently the
23 /// list of waypoints must have at least two valid (related to the
24 /// DirectedWaypoint's ignition::rndf::UniqueId) waypoints to derive an
25 /// interpolated geometry from them. The order the waypoints are set is the
26 /// way they will be used so create an interpolated curve (base line of the
27 /// Lane). Lane's width is fixed along the complete path length.
28 /// Builder class will receive a group of Connections that belong to the same
29 /// RNDF segment. Note that RNDF, unlike Maliput, has no constraint on lane
30 /// direction inside a segment, so Connections should be marked (using
31 /// the constructor's inverse_direction parameter) so they can be grouped by
32 /// sense of direction. To define the direction, one of the Connections in the
33 /// group will be flagged as the reference, and an interpolated spline is
34 /// computed from the the DirectedWaypoints within the reference Connection to
35 /// obtain the tangent information.
36 class Connection {
37  public:
39  /// Constructs a Connection.
40  /// @param id Connection's ID.
41  /// @param waypoints A DirectedWaypoints vector that determines the lane path.
42  /// @param width The Lane's width.
43  /// @param inverse_direction False if this Connection belongs to a group of
44  /// Connections and the sense of direction of this Connection is the same as
45  /// the first Connection in the group. The first Connection will be set False
46  /// as default.
47  /// @throws std::runtime_error When the number of @p waypoints items, whose
48  /// ignition::rndf::UniqueId is valid, is less than 2.
49  /// @throws std::runtime_error When the @p width is less or equal to 0.0.
50  Connection(const std::string& id,
51  const std::vector<DirectedWaypoint>& waypoints, double width,
52  bool inverse_direction)
53  : id_(id),
54  waypoints_(waypoints),
55  width_(width),
56  inverse_direction_(inverse_direction) {
57  ThrowIfInvalidWaypoints(waypoints);
58  DRAKE_THROW_UNLESS(width > 0.0);
59  }
60 
61  /// Getter of the ID.
62  /// @return The ID.
63  const std::string& id() const { return id_; }
64 
65  /// Getter of the first item in the vector returned by waypoints().
66  /// @return A constant reference to the first item in the vector returned by
67  /// waypoints().
68  const DirectedWaypoint& start() const { return waypoints_.front(); }
69 
70  /// Getter of the last item in the vector returned by waypoints().
71  /// @return A constant reference to the last item in the vector returned by
72  /// waypoints().
73  const DirectedWaypoint& end() const { return waypoints_.back(); }
74 
75  /// Getter of the DirectedWaypoint vector.
76  /// @return The DirectedWaypoint vector.
77  const std::vector<DirectedWaypoint>& waypoints() const { return waypoints_; }
78 
79  /// Setter of the DirectedWaypoint vector.
80  /// @param waypoints A vector of DirectedWaypoints.
81  /// @throws std::runtime_error When @p waypoints holds less than two valid
82  /// DirectedWaypoints.
83  void set_waypoints(const std::vector<DirectedWaypoint>& waypoints) {
84  ThrowIfInvalidWaypoints(waypoints);
85  waypoints_ = waypoints;
86  }
87 
88  /// Adds a waypoint to the DirectedWaypoint vector at @p position index.
89  /// @param waypoint A DirectedWaypoint to add.
90  /// @param position The index in the DirectedWaypoint vector at which
91  /// @p waypoint should be added.
92  /// @throws std::runtime_error When position is less than 0.
93  /// @throws std::runtime_error When position is greater than the size of the
94  /// DirectedWaypoint vector.
95  void AddWaypoint(const DirectedWaypoint& waypoint, int position);
96 
97  /// Getter for the width.
98  /// @return The width.
99  double width() const { return width_; }
100 
101  /// Setter for the width.
102  /// @param width The absolute span from one lateral extent to the other of the
103  /// api::Lane that the Builder will make from this Connection.
104  /// @throws std::runtime_error When @p width is less or equal to zero.
105  void set_width(double width) {
106  DRAKE_THROW_UNLESS(width > 0.0);
107  width_ = width;
108  }
109 
110  /// Getter of inverse_direction.
111  /// @return true when this Connection has inverse direction relative to the
112  /// base Lane in the RNDF segment to which this Connection belongs, and false
113  /// otherwise.
114  bool inverse_direction() const { return inverse_direction_; }
115 
116  /// Setter of inverse_direction.
117  /// @brief inverse_direction The relative direction regarding another
118  /// Connection which will be considered as the reference direction for the
119  /// RNDF segment.
121  inverse_direction_ = inverse_direction;
122  }
123 
124  private:
125  // Throws an exception if @p waypoints doesn't hold at least two valid
126  // waypoints.
127  // @param waypoints A vector of DirectedWaypoints to check.
128  // @throws std::runtime_error When @p waypoints holds less than two valid
129  // DirectedWaypoints.
130  void ThrowIfInvalidWaypoints(
131  const std::vector<DirectedWaypoint>& waypoints) const {
132  int num_valid = 0;
133  for (const DirectedWaypoint& wp : waypoints) {
134  if (wp.id().Valid()) {
135  num_valid++;
136  if (num_valid == 2) {
137  return;
138  }
139  }
140  }
141  throw std::runtime_error("Insufficient valid waypoints.");
142  }
143 
144  // TODO(@agalbachicar) Uniqueness of the id_, when it is valid, should be
145  // verified and guaranteed.
146  // The ID.
147  std::string id_;
148  // A vector that holds the sequence of the DirectedWaypoints.
149  std::vector<DirectedWaypoint> waypoints_;
150  // The width of the connection.
151  double width_{};
152  // A boolean that tells if this connection has an inverse direction relative
153  // to another connection in the same RNDF segment and that
154  // represents the first lane in the segment.
155  bool inverse_direction_{false};
156 };
157 
158 } // namespace rndf
159 } // namespace maliput
160 } // namespace drake
void set_inverse_direction(bool inverse_direction)
Setter of inverse_direction.
Definition: connection.h:120
Provides a convenient wrapper to throw an exception when a condition is unmet.
Definition: automotive_demo.cc:88
const double position
Definition: robot_plan_interpolator_test.cc:65
void AddWaypoint(const DirectedWaypoint &waypoint, int position)
Adds a waypoint to the DirectedWaypoint vector at position index.
Definition: connection.cc:7
STL namespace.
Definition: branch_point.cc:7
const DirectedWaypoint & end() const
Getter of the last item in the vector returned by waypoints().
Definition: connection.h:73
bool inverse_direction() const
Getter of inverse_direction.
Definition: connection.h:114
const std::vector< DirectedWaypoint > & waypoints() const
Getter of the DirectedWaypoint vector.
Definition: connection.h:77
#define DRAKE_THROW_UNLESS(condition)
Evaluates condition and iff the value is false will throw an exception with a message showing at leas...
Definition: drake_throw.h:23
A container that holds the information needed by a Builder to construct a Lane from a sequence of ign...
Definition: directed_waypoint.h:17
std::vector< double > vector
Definition: translator_test.cc:20
void set_waypoints(const std::vector< DirectedWaypoint > &waypoints)
Setter of the DirectedWaypoint vector.
Definition: connection.h:83
#define DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(Classname)
DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN defaults the special member functions for copy-construction, copy-assignment, move-construction, and move-assignment.
Definition: drake_copyable.h:57
const DirectedWaypoint & start() const
Getter of the first item in the vector returned by waypoints().
Definition: connection.h:68
const std::string & id() const
Getter of the ID.
Definition: connection.h:63
double width() const
Getter for the width.
Definition: connection.h:99
A container that holds the information needed by a Builder to construct one or more Lane objects...
Definition: connection.h:36
Provides careful macros to selectively enable or disable the special member functions for copy-constr...
void set_width(double width)
Setter for the width.
Definition: connection.h:105