ilqgames
A new real-time solver for large-scale differential games.
quadratic_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 // Quadratic penalty on distance from a given Polyline2.
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 #include <ilqgames/cost/quadratic_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 QuadraticPolyline2Cost::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 
62  if (is_endpoint) {
63  // If the is_endpoint flag is raised, we set the signed_squared_distance to
64  // 0.0.
65  signed_squared_distance = 0.0;
66  }
67 
68  return 0.5 * weight_ * std::abs(signed_squared_distance);
69 }
70 
71 void QuadraticPolyline2Cost::Quadraticize(const VectorXf& input, MatrixXf* hess,
72  VectorXf* grad) const {
73  CHECK_LT(xidx_, input.size());
74  CHECK_LT(yidx_, input.size());
75 
76  CHECK_NOTNULL(hess);
77  CHECK_NOTNULL(grad);
78  CHECK_EQ(input.size(), hess->rows());
79  CHECK_EQ(input.size(), hess->cols());
80  CHECK_EQ(input.size(), grad->size());
81 
82  // Unpack current position and find closest point / segment.
83  const Point2 current_position(input(xidx_), input(yidx_));
84 
85  bool is_vertex;
86  bool is_endpoint;
87  LineSegment2 segment(Point2(0.0, 0.0), Point2(1.0, 1.0));
88  const Point2 closest_point = polyline_.ClosestPoint(
89  current_position, &is_vertex, &segment, nullptr, &is_endpoint);
90 
91  // First check whether the closest point is a endpoint of the polyline.
92  if (is_endpoint) return;
93 
94  // Handle cases separately depending on whether or not closest point is
95  // a vertex of the polyline.
96  float ddx = weight_;
97  float ddy = weight_;
98  float dxdy = 0.0;
99  float dx = weight_ * (current_position.x() - closest_point.x());
100  float dy = weight_ * (current_position.y() - closest_point.y());
101 
102  if (!is_vertex) {
103  const Point2 relative = current_position - segment.FirstPoint();
104  const Point2& unit_segment = segment.UnitDirection();
105 
106  // Handle Hessian first.
107  ddx = weight_ * unit_segment.y() * unit_segment.y();
108  ddy = weight_ * unit_segment.x() * unit_segment.x();
109  dxdy = -weight_ * unit_segment.x() * unit_segment.y();
110 
111  // Handle gradient.
112  const float w_cross = weight_ * (relative.x() * unit_segment.y() -
113  relative.y() * unit_segment.x());
114 
115  dx = w_cross * unit_segment.y();
116  dy = -w_cross * unit_segment.x();
117  }
118 
119  (*grad)(xidx_) += dx;
120  (*grad)(yidx_) += dy;
121 
122  (*hess)(xidx_, xidx_) += ddx;
123  (*hess)(yidx_, yidx_) += ddy;
124  (*hess)(xidx_, yidx_) += dxdy;
125  (*hess)(yidx_, xidx_) += dxdy;
126 }
127 
128 } // namespace ilqgames