ilqgames
A new real-time solver for large-scale differential games.
semiquadratic_polyline2_cost.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 // Semiquadratic cost on distance from a polyline.
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 #include <ilqgames/cost/semiquadratic_polyline2_cost.h>
44 #include <ilqgames/cost/time_invariant_cost.h>
45 #include <ilqgames/geometry/polyline2.h>
46 #include <ilqgames/utils/types.h>
47 
48 #include <tuple>
49 
50 namespace ilqgames {
51 
52 float SemiquadraticPolyline2Cost::Evaluate(const VectorXf& input) const {
53  CHECK_LT(xidx_, input.size());
54  CHECK_LT(yidx_, input.size());
55 
56  // Compute signed squared distance by finding closest point.
57  float signed_squared_distance;
58  bool is_endpoint;
59  polyline_.ClosestPoint(Point2(input(xidx_), input(yidx_)), nullptr, nullptr,
60  &signed_squared_distance, &is_endpoint);
61  if (is_endpoint) {
62  // If the is_endpoint flag is raised, we return 0.0.
63  return 0.0;
64  }
65  // Check which side we're on.
66  if (!IsActive(signed_squared_distance)) return 0.0;
67 
68  // Handle orientation.
69  const float signed_distance = sgn(signed_squared_distance) *
70  std::sqrt(std::abs(signed_squared_distance));
71  const float diff = signed_distance - threshold_;
72  return 0.5 * weight_ * diff * diff;
73 }
74 
75 void SemiquadraticPolyline2Cost::Quadraticize(const VectorXf& input,
76  MatrixXf* hess,
77  VectorXf* grad) const {
78  CHECK_LT(xidx_, input.size());
79  CHECK_LT(yidx_, input.size());
80 
81  CHECK_NOTNULL(hess);
82  CHECK_NOTNULL(grad);
83  CHECK_EQ(input.size(), hess->rows());
84  CHECK_EQ(input.size(), hess->cols());
85  CHECK_EQ(input.size(), grad->size());
86 
87  // Unpack current position and find closest point / segment.
88  const Point2 current_position(input(xidx_), input(yidx_));
89 
90  float signed_squared_distance;
91  bool is_vertex;
92  bool is_endpoint;
93  LineSegment2 segment(Point2(0.0, 0.0), Point2(1.0, 1.0));
94  const Point2 closest_point =
95  polyline_.ClosestPoint(current_position, &is_vertex, &segment,
96  &signed_squared_distance, &is_endpoint);
97 
98  // Check if cost is active.
99  if (!IsActive(signed_squared_distance)) return;
100 
101  // First checks whether the closest point is an endpoint of the polyline
102  if (is_endpoint) return;
103 
104  // Handle cases separately depending on whether or not closest point is
105  // a vertex of the polyline.
106  float ddx = weight_;
107  float ddy = weight_;
108  float dxdy = 0.0;
109 
110  float scaling = std::sqrt(std::abs(signed_squared_distance));
111  scaling = (scaling - std::abs(threshold_)) / scaling;
112  float dx = weight_ * scaling * (current_position.x() - closest_point.x());
113  float dy = weight_ * scaling * (current_position.y() - closest_point.y());
114  if (!is_vertex) {
115  const Point2 relative = current_position - segment.FirstPoint();
116  const Point2& unit_segment = segment.UnitDirection();
117 
118  // Handle Hessian first.
119  ddx = weight_ * unit_segment.y() * unit_segment.y();
120  ddy = weight_ * unit_segment.x() * unit_segment.x();
121 
122  const float cross_term = -weight_ * unit_segment.x() * unit_segment.y();
123  dxdy = cross_term;
124  dxdy = cross_term;
125 
126  // Handle gradient.
127  const float w_cross =
128  weight_ * (relative.x() * unit_segment.y() -
129  relative.y() * unit_segment.x() - threshold_);
130 
131  dx = w_cross * unit_segment.y();
132  dy = -w_cross * unit_segment.x();
133  }
134 
135  (*grad)(xidx_) += dx;
136  (*grad)(yidx_) += dy;
137 
138  (*hess)(xidx_, xidx_) += ddx;
139  (*hess)(yidx_, yidx_) += ddy;
140  (*hess)(xidx_, yidx_) += dxdy;
141  (*hess)(yidx_, xidx_) += dxdy;
142 }
143 
144 } // namespace ilqgames