Apollo 10.0
自动驾驶开放平台
vec2d.h
浏览该文件的文档.
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
22#pragma once
23
24#include <cmath>
25#include <string>
26
31namespace apollo {
32namespace common {
33namespace math {
34
35constexpr double kMathEpsilon = 1e-10;
36
42class Vec2d {
43 public:
45 constexpr Vec2d(const double x, const double y) noexcept : x_(x), y_(y) {}
46
48 constexpr Vec2d() noexcept : Vec2d(0, 0) {}
49
51 static Vec2d CreateUnitVec2d(const double angle);
52
54 double x() const { return x_; }
55
57 double y() const { return y_; }
58
60 void set_x(const double x) { x_ = x; }
61
63 void set_y(const double y) { y_ = y; }
64
66 double Length() const;
67
69 double LengthSquare() const;
70
72 double Angle() const;
73
75 void Normalize();
76
78 double DistanceTo(const Vec2d &other) const;
79
81 double DistanceSquareTo(const Vec2d &other) const;
82
84 double CrossProd(const Vec2d &other) const;
85
87 double InnerProd(const Vec2d &other) const;
88
90 Vec2d rotate(const double angle) const;
91
93 void SelfRotate(const double angle);
94
96 Vec2d operator+(const Vec2d &other) const;
97
99 Vec2d operator-(const Vec2d &other) const;
100
102 Vec2d operator*(const double ratio) const;
103
105 Vec2d operator/(const double ratio) const;
106
108 Vec2d &operator+=(const Vec2d &other);
109
111 Vec2d &operator-=(const Vec2d &other);
112
114 Vec2d &operator*=(const double ratio);
115
117 Vec2d &operator/=(const double ratio);
118
120 bool operator==(const Vec2d &other) const;
121
123 std::string DebugString() const;
124
125 protected:
126 double x_ = 0.0;
127 double y_ = 0.0;
128};
129
131Vec2d operator*(const double ratio, const Vec2d &vec);
132
133} // namespace math
134} // namespace common
135} // namespace apollo
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42
void set_x(const double x)
Setter for x component
Definition vec2d.h:60
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
constexpr Vec2d(const double x, const double y) noexcept
Constructor which takes x- and y-coordinates.
Definition vec2d.h:45
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
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
void set_y(const double y)
Setter for y component
Definition vec2d.h:63
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
constexpr double kMathEpsilon
Definition vec2d.h:35
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