ilqgames
A new real-time solver for large-scale differential games.
polyline2.h
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 // Polyline2 class for piecewise linear paths in 2D.
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 #ifndef ILQGAMES_GEOMETRY_POLYLINE2_H
44 #define ILQGAMES_GEOMETRY_POLYLINE2_H
45 
46 #include <ilqgames/geometry/line_segment2.h>
47 #include <ilqgames/utils/types.h>
48 
49 #include <glog/logging.h>
50 #include <math.h>
51 
52 namespace ilqgames {
53 
54 class Polyline2 {
55  public:
56  // Construct from a list of points. This list must contain at least 2 points!
57  Polyline2(const PointList2& points);
58  ~Polyline2() {}
59 
60  // Add a new point to the end of the polyline.
61  void AddPoint(const Point2& point);
62 
63  // Compute length.
64  float Length() const { return length_; }
65 
66  // Find closest point on this line segment to a given point, and optionally
67  // the line segment that point belongs to (and flag for whether it is a
68  // vertex), and the signed squared distance, where right is positive.
69  Point2 ClosestPoint(const Point2& query, bool* is_vertex = nullptr,
70  LineSegment2* segment = nullptr,
71  float* signed_squared_distance = nullptr,
72  bool* is_endpoint = nullptr) const;
73 
74  // Find the point the given distance from the start of the polyline.
75  // Optionally returns whether this is a vertex and the line segment which the
76  // point belongs to.
77  Point2 PointAt(float route_pos, bool* is_vertex = nullptr,
78  LineSegment2* segment = nullptr,
79  bool* is_endpoint = nullptr) const;
80 
81  // Access line segments.
82  const std::vector<LineSegment2>& Segments() const { return segments_; }
83 
84  private:
85  std::vector<LineSegment2> segments_;
86  std::vector<float> cumulative_lengths_;
87  float length_;
88 }; // struct Polyline2
89 
90 } // namespace ilqgames
91 
92 #endif