Apollo 10.0
自动驾驶开放平台
vec2d.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <cmath>
20
21#include "absl/strings/str_cat.h"
22
23#include "cyber/common/log.h"
24
25namespace apollo {
26namespace common {
27namespace math {
28
29Vec2d Vec2d::CreateUnitVec2d(const double angle) {
30 return Vec2d(std::cos(angle), std::sin(angle));
31}
32
33double Vec2d::Length() const { return std::hypot(x_, y_); }
34
35double Vec2d::LengthSquare() const { return x_ * x_ + y_ * y_; }
36
37double Vec2d::Angle() const { return std::atan2(y_, x_); }
38
40 const double l = Length();
41 if (l > kMathEpsilon) {
42 x_ /= l;
43 y_ /= l;
44 }
45}
46
47double Vec2d::DistanceTo(const Vec2d &other) const {
48 return std::hypot(x_ - other.x_, y_ - other.y_);
49}
50
51double Vec2d::DistanceSquareTo(const Vec2d &other) const {
52 const double dx = x_ - other.x_;
53 const double dy = y_ - other.y_;
54 return dx * dx + dy * dy;
55}
56
57double Vec2d::CrossProd(const Vec2d &other) const {
58 return x_ * other.y() - y_ * other.x();
59}
60
61double Vec2d::InnerProd(const Vec2d &other) const {
62 return x_ * other.x() + y_ * other.y();
63}
64
65Vec2d Vec2d::rotate(const double angle) const {
66 return Vec2d(x_ * cos(angle) - y_ * sin(angle),
67 x_ * sin(angle) + y_ * cos(angle));
68}
69
70void Vec2d::SelfRotate(const double angle) {
71 double tmp_x = x_;
72 x_ = x_ * cos(angle) - y_ * sin(angle);
73 y_ = tmp_x * sin(angle) + y_ * cos(angle);
74}
75
76Vec2d Vec2d::operator+(const Vec2d &other) const {
77 return Vec2d(x_ + other.x(), y_ + other.y());
78}
79
80Vec2d Vec2d::operator-(const Vec2d &other) const {
81 return Vec2d(x_ - other.x(), y_ - other.y());
82}
83
84Vec2d Vec2d::operator*(const double ratio) const {
85 return Vec2d(x_ * ratio, y_ * ratio);
86}
87
88Vec2d Vec2d::operator/(const double ratio) const {
89 CHECK_GT(std::abs(ratio), kMathEpsilon);
90 return Vec2d(x_ / ratio, y_ / ratio);
91}
92
94 x_ += other.x();
95 y_ += other.y();
96 return *this;
97}
98
100 x_ -= other.x();
101 y_ -= other.y();
102 return *this;
103}
104
105Vec2d &Vec2d::operator*=(const double ratio) {
106 x_ *= ratio;
107 y_ *= ratio;
108 return *this;
109}
110
111Vec2d &Vec2d::operator/=(const double ratio) {
112 CHECK_GT(std::abs(ratio), kMathEpsilon);
113 x_ /= ratio;
114 y_ /= ratio;
115 return *this;
116}
117
118bool Vec2d::operator==(const Vec2d &other) const {
119 return (std::abs(x_ - other.x()) < kMathEpsilon &&
120 std::abs(y_ - other.y()) < kMathEpsilon);
121}
122
123Vec2d operator*(const double ratio, const Vec2d &vec) { return vec * ratio; }
124
125std::string Vec2d::DebugString() const {
126 return absl::StrCat("vec2d ( x = ", x_, " y = ", y_, " )");
127}
128
129} // namespace math
130} // namespace common
131} // namespace apollo
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42
double LengthSquare() const
Gets the squared length of the vector
Definition vec2d.cc:35
void SelfRotate(const double angle)
rotate the vector itself by angle.
Definition vec2d.cc:70
double DistanceSquareTo(const Vec2d &other) const
Returns the squared distance to the given vector
Definition vec2d.cc:51
Vec2d operator*(const double ratio) const
Multiplies Vec2d by a scalar
Definition vec2d.cc:84
static Vec2d CreateUnitVec2d(const double angle)
Creates a unit-vector with a given angle to the positive x semi-axis
Definition vec2d.cc:29
Vec2d & operator-=(const Vec2d &other)
Subtracts another Vec2d to the current one
Definition vec2d.cc:99
Vec2d & operator*=(const double ratio)
Multiplies this Vec2d by a scalar
Definition vec2d.cc:105
double InnerProd(const Vec2d &other) const
Returns the inner product between these two Vec2d.
Definition vec2d.cc:61
Vec2d operator/(const double ratio) const
Divides Vec2d by a scalar
Definition vec2d.cc:88
double Length() const
Gets the length of the vector
Definition vec2d.cc:33
Vec2d operator+(const Vec2d &other) const
Sums two Vec2d
Definition vec2d.cc:76
std::string DebugString() const
Returns a human-readable string representing this object
Definition vec2d.cc:125
double y() const
Getter for y component
Definition vec2d.h:57
Vec2d rotate(const double angle) const
rotate the vector by angle.
Definition vec2d.cc:65
double Angle() const
Gets the angle between the vector and the positive x semi-axis
Definition vec2d.cc:37
constexpr Vec2d() noexcept
Constructor returning the zero vector.
Definition vec2d.h:48
double DistanceTo(const Vec2d &other) const
Returns the distance to the given vector
Definition vec2d.cc:47
double x() const
Getter for x component
Definition vec2d.h:54
Vec2d & operator/=(const double ratio)
Divides this Vec2d by a scalar
Definition vec2d.cc:111
bool operator==(const Vec2d &other) const
Compares two Vec2d
Definition vec2d.cc:118
double CrossProd(const Vec2d &other) const
Returns the "cross" product between these two Vec2d (non-standard).
Definition vec2d.cc:57
void Normalize()
Returns the unit vector that is co-linear with this vector
Definition vec2d.cc:39
Vec2d & operator+=(const Vec2d &other)
Sums another Vec2d to the current one
Definition vec2d.cc:93
Vec2d operator-(const Vec2d &other) const
Subtracts two Vec2d
Definition vec2d.cc:80
float cos(Angle16 a)
Definition angle.cc:42
constexpr double kMathEpsilon
Definition vec2d.h:35
float sin(Angle16 a)
Definition angle.cc:25
Angle< T > operator*(Angle< T > lhs, Scalar rhs)
Multiplies an Angle by a scalar
Definition angle.h:208
class register implement
Definition arena_queue.h:37
Defines the Vec2d class.