Drake
multilane_types_compare.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <gmock/gmock.h>
4 #include <gtest/gtest.h>
5 
6 #include "drake/automotive/maliput/api/test_utilities/maliput_types_compare.h"
7 #include "drake/automotive/maliput/multilane/builder.h"
8 #include "drake/automotive/maliput/multilane/connection.h"
9 
10 namespace drake {
11 namespace maliput {
12 namespace multilane {
13 namespace test {
14 
15 using ::testing::Matcher;
16 using ::testing::MatcherInterface;
17 using ::testing::MatchResultListener;
18 
19 // Compares equality within @p tolerance deviation of two EndpointXy objects.
20 // @param xy1 An EndpointXy object to compare.
21 // @param xy2 An EndpointXy object to compare.
22 // @param tolerance An allowable absolute deviation for each EndpointXy's
23 // coordinate.
24 // @return ::testing::AssertionFailure() When EndpointXy objects are different.
25 // @return ::testing::AssertionSuccess() When EndpointXy objects are within
26 // the @p tolerance deviation.
27 
28 // TODO(agalbachicar) Given that EndpointXy is composed of two lengths and
29 // one angle, tolerance must be replaced into two distinct
30 // values to match each magnitude.
31 ::testing::AssertionResult IsEndpointXyClose(const EndpointXy& xy1,
32  const EndpointXy& xy2,
33  double tolerance);
34 
35 // Compares equality within @p tolerance deviation of two EndpointZ objects.
36 // @param z1 An EndpointZ object to compare.
37 // @param z2 An EndpointZ object to compare.
38 // @param tolerance An allowable absolute deviation for each EndpointZ's
39 // coordinate.
40 // @return ::testing::AssertionFailure() When EndpointZ objects are different.
41 // @return ::testing::AssertionSuccess() When EndpointZ objects are within
42 // the @p tolerance deviation.
43 
44 // TODO(agalbachicar) Given that EndpointZ is composed of different
45 // magnitudes, tolerance must be replaced into distinct
46 // values to match each magnitude.
47 ::testing::AssertionResult IsEndpointZClose(const EndpointZ& z1,
48  const EndpointZ& z2,
49  double tolerance);
50 
51 // Compares equality within @p tolerance deviation of two Endpoint objects.
52 // @param pos1 An Endpoint object to compare.
53 // @param pos2 An Endpoint object to compare.
54 // @param tolerance An allowable absolute deviation for each Endpoint's
55 // coordinate.
56 // @return ::testing::AssertionFailure() When Endpoint objects are different.
57 // @return ::testing::AssertionSuccess() When Endpoint objects are within
58 // the @p tolerance deviation.
59 
60 // TODO(agalbachicar) Given that EndpointXy and EndpointZ are composed of
61 // different magnitudes, tolerance must be replaced into
62 // distinct values to match each magnitude.
63 ::testing::AssertionResult IsEndpointClose(const Endpoint& pos1,
64  const Endpoint& pos2,
65  double tolerance);
66 
67 // Compares equality within @p linear_tolerance and @p angular_tolerance
68 // deviations of two ArcOffset objects.
69 // @param arc_offset1 An ArcOffset object to compare.
70 // @param arc_offset2 An ArcOffset object to compare.
71 // @param linear_tolerance An allowable absolute linear deviation for
72 // ArcOffset's radius.
73 // @param angular_tolerance An allowable absolute angle deviation for
74 // ArcOffset's d_theta.
75 // @return ::testing::AssertionFailure() When ArcOffset objects are different.
76 // @return ::testing::AssertionSuccess() When ArcOffset objects are within
77 // @p linear_tolerance and @p angular_tolerance deviations.
78 ::testing::AssertionResult IsArcOffsetClose(const ArcOffset& arc_offset1,
79  const ArcOffset& arc_offset2,
80  double linear_tolerance,
81  double angular_tolerance);
82 
83 /// Wraps api::HBounds comparison into a MatcherInterface.
84 class HBoundsMatcher : public MatcherInterface<const api::HBounds&> {
85  public:
86  HBoundsMatcher(const api::HBounds& elevation_bounds, double tolerance)
87  : elevation_bounds_(elevation_bounds), tolerance_(tolerance) {}
88 
89  bool MatchAndExplain(const api::HBounds& other,
90  MatchResultListener*) const override {
91  return api::test::IsHBoundsClose(elevation_bounds_, other, tolerance_);
92  }
93 
94  void DescribeTo(std::ostream* os) const override {
95  *os << "is within tolerance [" << tolerance_ << "] of elevation_bounds: ["
96  << elevation_bounds_.min() << ", " << elevation_bounds_.max() << "].";
97  }
98 
99  private:
100  const api::HBounds elevation_bounds_;
101  const double tolerance_{};
102 };
103 
104 /// @return A Matcher<const api::HBounds&> of type HBoundsMatcher.
105 Matcher<const api::HBounds&> Matches(const api::HBounds& elevation_bounds,
106  double tolerance);
107 
108 /// Wraps an ArcOffset comparison into a MatcherInterface.
109 class ArcOffsetMatcher : public MatcherInterface<const ArcOffset&> {
110  public:
111  ArcOffsetMatcher(const ArcOffset& arc_offset, double linear_tolerance,
112  double angular_tolerance)
113  : arc_offset_(arc_offset),
114  linear_tolerance_(linear_tolerance),
115  angular_tolerance_(angular_tolerance) {}
116 
117  bool MatchAndExplain(const ArcOffset& other,
118  MatchResultListener*) const override {
119  return IsArcOffsetClose(arc_offset_, other, linear_tolerance_,
120  angular_tolerance_);
121  }
122 
123  void DescribeTo(std::ostream* os) const override {
124  *os << "is within linear and angular tolerance: [" << linear_tolerance_
125  << ", " << angular_tolerance_ << "] of arc_offset: ["
126  << arc_offset_.radius() << ", " << arc_offset_.d_theta() << "].";
127  }
128 
129  private:
130  const ArcOffset arc_offset_;
131  const double linear_tolerance_{};
132  const double angular_tolerance_{};
133 };
134 
135 /// @return A Matcher<const ArcOffset&> of type ArcOffsetMatcher.
136 Matcher<const ArcOffset&> Matches(const ArcOffset& arc_offset,
137  double linear_tolerance,
138  double angular_tolerance);
139 
140 /// Wraps a LineOffset comparison into a MatcherInterface.
141 class LineOffsetMatcher : public MatcherInterface<const LineOffset&> {
142  public:
143  LineOffsetMatcher(const LineOffset& line_offset, double tolerance)
144  : line_offset_(line_offset), tolerance_(tolerance) {}
145 
146  bool MatchAndExplain(const LineOffset& other,
147  MatchResultListener*) const override {
148  const double delta = std::abs(line_offset_.length() - other.length());
149  return delta <= tolerance_;
150  }
151 
152  void DescribeTo(std::ostream* os) const override {
153  *os << "is within tolerance: [" << tolerance_ << "] of line_offset: ["
154  << line_offset_ << "].";
155  }
156 
157  private:
158  const LineOffset line_offset_{};
159  const double tolerance_{};
160 };
161 
162 /// @return A Matcher<const LineOffset&> of type LineOffsetMatcher.
163 Matcher<const LineOffset&> Matches(const LineOffset& line_offset,
164  double tolerance);
165 
166 /// Wraps a LineOffset comparison into a MatcherInterface.
167 class LaneLayoutMatcher : public MatcherInterface<const LaneLayout&> {
168  public:
169  LaneLayoutMatcher(const LaneLayout& lane_layout, double tolerance)
170  : lane_layout_(lane_layout), tolerance_(tolerance) {}
171 
172  bool MatchAndExplain(const LaneLayout& other,
173  MatchResultListener*) const override {
174  double delta{};
175 
176  delta = std::abs(lane_layout_.left_shoulder() - other.left_shoulder());
177  if (delta > tolerance_) return false;
178 
179  delta = std::abs(lane_layout_.right_shoulder() - other.right_shoulder());
180  if (delta > tolerance_) return false;
181 
182  if (lane_layout_.num_lanes() != other.num_lanes()) return false;
183 
184  if (lane_layout_.ref_lane() != other.ref_lane()) return false;
185 
186  delta = std::abs(lane_layout_.ref_r0() - other.ref_r0());
187  if (delta > tolerance_) return false;
188 
189  return true;
190  }
191 
192  void DescribeTo(std::ostream* os) const override {
193  *os << "is within tolerance: [" << tolerance_ << "] of lane_layout: ["
194  << lane_layout_ << "].";
195  }
196 
197  private:
198  const LaneLayout lane_layout_;
199  const double tolerance_{};
200 };
201 
202 /// @return A Matcher<const LaneLayout&> of type LaneLayoutMatcher.
203 Matcher<const LaneLayout&> Matches(const LaneLayout& lane_layout,
204  double tolerance);
205 
206 /// Wraps a StartReference::Spec comparison into a MatcherInterface.
208  : public MatcherInterface<const StartReference::Spec&> {
209  public:
211  double tolerance)
212  : start_reference_(start_reference), tolerance_(tolerance) {}
213 
215  MatchResultListener*) const override {
216  return IsEndpointClose(start_reference_.endpoint(), other.endpoint(),
217  tolerance_);
218  }
219 
220  void DescribeTo(std::ostream* os) const override {
221  *os << "is within tolerance: [" << tolerance_ << "] of start_reference: [{"
222  << start_reference_.endpoint() << "}].";
223  }
224 
225  private:
226  const StartReference::Spec start_reference_;
227  const double tolerance_{};
228 };
229 
230 /// @return A Matcher<const StartReference::Spec&> of type
231 /// StartReferenceSpecMatcher.
232 Matcher<const StartReference::Spec&> Matches(
233  const StartReference::Spec& start_reference, double tolerance);
234 
235 /// Wraps a EndReference::Spec comparison into a MatcherInterface.
237  : public MatcherInterface<const EndReference::Spec&> {
238  public:
240  double tolerance)
241  : end_reference_(end_reference), tolerance_(tolerance) {}
242 
244  MatchResultListener*) const override {
245  return IsEndpointZClose(end_reference_.endpoint_z(), other.endpoint_z(),
246  tolerance_);
247  }
248 
249  void DescribeTo(std::ostream* os) const override {
250  *os << "is within tolerance: [" << tolerance_ << "] of end_reference: [{"
251  << end_reference_.endpoint_z() << "}].";
252  }
253 
254  private:
255  const EndReference::Spec end_reference_;
256  const double tolerance_{};
257 };
258 
259 /// @return A Matcher<const EndReference::Spec&> of type
260 /// EndReferenceSpecMatcher.
261 Matcher<const EndReference::Spec&> Matches(
262  const EndReference::Spec& end_reference, double tolerance);
263 
264 } // namespace test
265 } // namespace multilane
266 } // namespace maliput
267 } // namespace drake
::testing::AssertionResult IsHBoundsClose(const HBounds &hbounds1, const HBounds &hbounds2, double tolerance)
Definition: maliput_types_compare.cc:184
double ref_r0() const
Definition: builder.h:160
void DescribeTo(std::ostream *os) const override
Definition: multilane_types_compare.h:94
bool MatchAndExplain(const api::HBounds &other, MatchResultListener *) const override
Definition: multilane_types_compare.h:89
double right_shoulder() const
Definition: builder.h:154
::testing::AssertionResult IsEndpointZClose(const EndpointZ &z1, const EndpointZ &z2, double tolerance)
Definition: multilane_types_compare.cc:57
Definition: agent_trajectory.cc:5
bool MatchAndExplain(const StartReference::Spec &other, MatchResultListener *) const override
Definition: multilane_types_compare.h:214
Expression abs(const Expression &e)
Definition: symbolic_expression.cc:551
Defines how a Connection&#39;s reference curve starts.
Definition: builder.h:32
Wraps api::HBounds comparison into a MatcherInterface.
Definition: multilane_types_compare.h:84
LineOffsetMatcher(const LineOffset &line_offset, double tolerance)
Definition: multilane_types_compare.h:143
bool MatchAndExplain(const LineOffset &other, MatchResultListener *) const override
Definition: multilane_types_compare.h:146
void DescribeTo(std::ostream *os) const override
Definition: multilane_types_compare.h:220
void DescribeTo(std::ostream *os) const override
Definition: multilane_types_compare.h:152
StartReferenceSpecMatcher(const StartReference::Spec &start_reference, double tolerance)
Definition: multilane_types_compare.h:210
double length() const
Definition: connection.h:154
Wraps a EndReference::Spec comparison into a MatcherInterface.
Definition: multilane_types_compare.h:236
Wraps all the lane-related specifications in a Connection.
Definition: builder.h:123
double left_shoulder() const
Definition: builder.h:152
double max() const
Gets maximum bound.
Definition: lane_data.h:386
bool MatchAndExplain(const ArcOffset &other, MatchResultListener *) const override
Definition: multilane_types_compare.h:117
void DescribeTo(std::ostream *os) const override
Definition: multilane_types_compare.h:249
Definition: arc_road_curve.cc:11
double min() const
Gets minimum bound.
Definition: lane_data.h:378
Wraps a LineOffset comparison into a MatcherInterface.
Definition: multilane_types_compare.h:167
int num_lanes() const
Definition: builder.h:156
Specification for path offset along a line.
Definition: connection.h:144
const api::HBounds elevation_bounds
Definition: multilane_line_road_curve_test.cc:28
void DescribeTo(std::ostream *os) const override
Definition: multilane_types_compare.h:123
Wraps a LineOffset comparison into a MatcherInterface.
Definition: multilane_types_compare.h:141
::testing::AssertionResult IsEndpointXyClose(const EndpointXy &xy1, const EndpointXy &xy2, double tolerance)
Definition: multilane_types_compare.cc:12
::testing::AssertionResult IsArcOffsetClose(const ArcOffset &arc_offset1, const ArcOffset &arc_offset2, double linear_tolerance, double angular_tolerance)
Definition: multilane_types_compare.cc:143
HBoundsMatcher(const api::HBounds &elevation_bounds, double tolerance)
Definition: multilane_types_compare.h:86
Bounds in the elevation dimension (h component) of a Lane-frame, consisting of a pair of minimum and ...
Definition: lane_data.h:360
double tolerance_
Definition: model_test.cc:276
int ref_lane() const
Definition: builder.h:158
Wraps a StartReference::Spec comparison into a MatcherInterface.
Definition: multilane_types_compare.h:207
Defines how a Connection&#39;s reference curve ends.
Definition: builder.h:80
void DescribeTo(std::ostream *os) const override
Definition: multilane_types_compare.h:192
Specification for path offset along a circular arc.
Definition: connection.h:170
Wraps an ArcOffset comparison into a MatcherInterface.
Definition: multilane_types_compare.h:109
Matcher< const api::HBounds & > Matches(const api::HBounds &elevation_bounds, double tolerance)
Definition: multilane_types_compare.cc:181
ArcOffsetMatcher(const ArcOffset &arc_offset, double linear_tolerance, double angular_tolerance)
Definition: multilane_types_compare.h:111
const EndpointZ & endpoint_z() const
Definition: builder.h:84
EndReferenceSpecMatcher(const EndReference::Spec &end_reference, double tolerance)
Definition: multilane_types_compare.h:239
LaneLayoutMatcher(const LaneLayout &lane_layout, double tolerance)
Definition: multilane_types_compare.h:169
bool MatchAndExplain(const EndReference::Spec &other, MatchResultListener *) const override
Definition: multilane_types_compare.h:243
bool MatchAndExplain(const LaneLayout &other, MatchResultListener *) const override
Definition: multilane_types_compare.h:172
const Endpoint & endpoint() const
Definition: builder.h:36
::testing::AssertionResult IsEndpointClose(const Endpoint &p1, const Endpoint &p2, double tolerance)
Definition: multilane_types_compare.cc:112