ilqgames
A new real-time solver for large-scale differential games.
polyline2_signed_distance_cost.cpp
1 /*
2  * Copyright (c) 2020, 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 // Signed distance from a given polyline.
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 #include <ilqgames/cost/polyline2_signed_distance_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 Polyline2SignedDistanceCost::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  polyline_.ClosestPoint(Point2(input(xidx_), input(yidx_)), nullptr, nullptr,
59  &signed_squared_distance);
60  if (!oriented_same_as_polyline_) signed_squared_distance *= -1.0;
61 
62  return sgn(signed_squared_distance) *
63  std::sqrt(std::abs(signed_squared_distance)) -
64  nominal_;
65 }
66 
67 void Polyline2SignedDistanceCost::Quadraticize(const VectorXf& input,
68  MatrixXf* hess,
69  VectorXf* grad) const {
70  CHECK_LT(xidx_, input.size());
71  CHECK_LT(yidx_, input.size());
72 
73  CHECK_NOTNULL(hess);
74  CHECK_NOTNULL(grad);
75  CHECK_EQ(input.size(), hess->rows());
76  CHECK_EQ(input.size(), hess->cols());
77  CHECK_EQ(input.size(), grad->size());
78 
79  // Unpack current position and find closest point / segment.
80  const Point2 current_position(input(xidx_), input(yidx_));
81 
82  bool is_vertex;
83  float signed_squared_distance;
84  LineSegment2 segment(Point2(0.0, 0.0), Point2(1.0, 1.0));
85  const Point2 closest_point = polyline_.ClosestPoint(
86  current_position, &is_vertex, &segment, &signed_squared_distance);
87  if (!oriented_same_as_polyline_) signed_squared_distance *= -1.0;
88 
89  const float sign = sgn(signed_squared_distance);
90  const float distance = std::sqrt(std::abs(signed_squared_distance));
91  const float delta_x = current_position.x() - closest_point.x();
92  const float delta_y = current_position.y() - closest_point.y();
93 
94  // Handle cases separately depending on whether or not closest point is
95  // a vertex of the polyline.
96  float dx = sign * delta_x / distance;
97  float dy = sign * delta_y / distance;
98 
99  const float denom = signed_squared_distance * distance;
100  float ddx = delta_y * delta_y / denom;
101  float ddy = delta_x * delta_x / denom;
102  float dxdy = -delta_x * delta_y / denom;
103 
104  if (!is_vertex) {
105  const Point2& unit_segment = segment.UnitDirection();
106 
107  dx = unit_segment.y();
108  dy = -unit_segment.x();
109  ddx = 0.0;
110  ddy = 0.0;
111  dxdy = 0.0;
112  }
113 
114  (*grad)(xidx_) += dx;
115  (*grad)(yidx_) += dy;
116 
117  (*hess)(xidx_, xidx_) += ddx;
118  (*hess)(yidx_, yidx_) += ddy;
119  (*hess)(xidx_, yidx_) += dxdy;
120  (*hess)(yidx_, xidx_) += dxdy;
121 }
122 
123 } // namespace ilqgames