eigen_line.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, ATR, Atsushi Watanabe
3  * Copyright (c) 2014-2018, the neonavigation authors
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef TRAJECTORY_TRACKER_EIGEN_LINE_H
32 #define TRAJECTORY_TRACKER_EIGEN_LINE_H
33 
34 #include <cmath>
35 
36 #include <Eigen/Core>
37 
38 namespace trajectory_tracker
39 {
40 inline float curv3p(
41  const Eigen::Vector2d& a,
42  const Eigen::Vector2d& b,
43  const Eigen::Vector2d& c)
44 {
45  float ret;
46  ret = 2 * (a[0] * b[1] + b[0] * c[1] + c[0] * a[1] -
47  a[0] * c[1] - b[0] * a[1] - c[0] * b[1]);
48  ret /= std::sqrt((b - a).squaredNorm() * (b - c).squaredNorm() * (c - a).squaredNorm());
49 
50  return ret;
51 }
52 
53 inline float cross2(const Eigen::Vector2d& a, const Eigen::Vector2d& b)
54 {
55  return a[0] * b[1] - a[1] * b[0];
56 }
57 
58 inline float lineDistance(
59  const Eigen::Vector2d& a,
60  const Eigen::Vector2d& b,
61  const Eigen::Vector2d& c)
62 {
63  return cross2((b - a), (c - a)) / (b - a).norm();
64 }
65 
66 // lineStripDistanceSigned returns signed distance from the line strip.
67 // If c is behind a, negative value will be returned. [ (c) (a)---(b) ]
68 // Otherwise, positive value will be returned. [ (a)---(b) (c) ] [ (a)--(c)--(b) ]
70  const Eigen::Vector2d& a,
71  const Eigen::Vector2d& b,
72  const Eigen::Vector2d& c)
73 {
74  if ((b - a).dot(c - a) <= 0)
75  return -(c - a).norm();
76  if ((a - b).dot(c - b) <= 0)
77  return (c - b).norm();
78  return std::abs(lineDistance(a, b, c));
79 }
80 
81 inline float lineStripDistance(
82  const Eigen::Vector2d& a,
83  const Eigen::Vector2d& b,
84  const Eigen::Vector2d& c)
85 {
86  return std::abs(lineStripDistanceSigned(a, b, c));
87 }
88 
89 inline Eigen::Vector2d projection2d(
90  const Eigen::Vector2d& a,
91  const Eigen::Vector2d& b,
92  const Eigen::Vector2d& c)
93 {
94  const float r = (b - a).dot(c - a) / (b - a).squaredNorm();
95  return b * r + a * (1.0 - r);
96 }
97 } // namespace trajectory_tracker
98 
99 #endif // TRAJECTORY_TRACKER_EIGEN_LINE_H
doubleAcc dot(const VectorAcc &lhs, const VectorAcc &rhs)
float lineStripDistanceSigned(const Eigen::Vector2d &a, const Eigen::Vector2d &b, const Eigen::Vector2d &c)
Definition: eigen_line.h:69
float cross2(const Eigen::Vector2d &a, const Eigen::Vector2d &b)
Definition: eigen_line.h:53
float lineDistance(const Eigen::Vector2d &a, const Eigen::Vector2d &b, const Eigen::Vector2d &c)
Definition: eigen_line.h:58
float curv3p(const Eigen::Vector2d &a, const Eigen::Vector2d &b, const Eigen::Vector2d &c)
Definition: eigen_line.h:40
Eigen::Vector2d projection2d(const Eigen::Vector2d &a, const Eigen::Vector2d &b, const Eigen::Vector2d &c)
Definition: eigen_line.h:89
float lineStripDistance(const Eigen::Vector2d &a, const Eigen::Vector2d &b, const Eigen::Vector2d &c)
Definition: eigen_line.h:81


trajectory_tracker
Author(s): Atsushi Watanabe
autogenerated on Wed May 12 2021 02:20:40