Coverage Control Library
Loading...
Searching...
No Matches
vec2d.h
Go to the documentation of this file.
1/*
2 * This file is part of the CoverageControl library
3 *
4 * Author: Saurav Agarwal
5 * Contact: sauravag@seas.upenn.edu, agr.saurav1@gmail.com
6 * Repository: https://github.com/KumarRobotics/CoverageControl
7 *
8 * Copyright (c) 2024, Saurav Agarwal
9 *
10 * The CoverageControl library is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or (at your
13 * option) any later version.
14 *
15 * The CoverageControl library is distributed in the hope that it will be
16 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
18 * Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * CoverageControl library. If not, see <https://www.gnu.org/licenses/>.
22 */
23
29#ifndef CPPSRC_CORE_INCLUDE_COVERAGECONTROL_VEC2D_H_
30#define CPPSRC_CORE_INCLUDE_COVERAGECONTROL_VEC2D_H_
31
32#include <cmath>
33
35
36namespace CoverageControl {
37
45class Vec2d {
46 private:
47 double x_;
48 double y_;
49
50 public:
51 Vec2d() : x_{0}, y_{0} {}
52 Vec2d(double const x_i, double const y_i) : x_{x_i}, y_{y_i} {}
53
54 double x() const { return x_; }
55 double y() const { return y_; }
56
57 void SetX(double x) { x_ = x; }
58 void SetY(double y) { y_ = y; }
59
62 Vec2d v_perpendicular;
63 v_perpendicular = Vec2d(-y_, x_);
64 return v_perpendicular;
65 }
66
68 void Add(Vec2d const &v) {
69 x_ += v.x();
70 y_ += v.y();
71 }
72
74 int Divide(const double scalar) {
75 if (std::abs(scalar) < kEps) {
76 return 1;
77 }
78 x_ = x_ / scalar;
79 y_ = y_ / scalar;
80 return 0;
81 }
82
84 double Dot(Vec2d const &v) const { return v.x() * x_ + v.y() * y_; }
85
87 double NormSqr() const { return x_ * x_ + y_ * y_; }
88
90 double Norm() const { return std::sqrt(NormSqr()); }
91
93 int CosAngle(Vec2d const &v, double &ang) const {
94 if (std::abs(Norm()) < 1e-10 || std::abs(v.Norm()) < 1e-10) return 1;
95 ang = Dot(v) / (Norm() * v.Norm());
96 return 0;
97 }
98
100 double DistSqr(Vec2d const &v) const {
101 double del_x = x_ - v.x();
102 double del_y = y_ - v.y();
103 double dSqr = (del_x * del_x + del_y * del_y);
104 return dSqr;
105 }
106
107 double Dist(Vec2d const &v) const { return std::sqrt(DistSqr(v)); }
108
110 void DistTht(Vec2d const &v, double &d, double &tht) const {
111 d = std::sqrt(DistSqr(v));
112 double del_x = -x_ + v.x();
113 double del_y = -y_ + v.y();
114 tht = std::atan2(del_y, del_x);
115 if (tht < 0) tht += M_PI;
116 }
117
118 Vec2d operator+(Vec2d const &vec) const {
119 return Vec2d(x_ + vec.x(), y_ + vec.y());
120 }
121
122 Vec2d operator-(Vec2d const &vec) const {
123 return Vec2d(x_ - vec.x(), y_ - vec.y());
124 }
125
126 Vec2d operator-() const { return Vec2d(-x_, -y_); }
127
128 Vec2d operator/(double const &scalar) const {
129 return Vec2d(x_ / scalar, y_ / scalar);
130 }
131
132 Vec2d operator*(double const &scalar) const {
133 return Vec2d(x_ * scalar, y_ * scalar);
134 }
135
136 int Normalize() {
137 double norm = Norm();
138 if (norm < kEps) {
139 return 1;
140 }
141 x_ = x_ / norm;
142 y_ = y_ / norm;
143 return 0;
144 }
145};
146
147} /* namespace CoverageControl */
148#endif // CPPSRC_CORE_INCLUDE_COVERAGECONTROL_VEC2D_H_
A class for 2D vector.
Definition vec2d.h:45
int Divide(const double scalar)
Divide vector by a scalar.
Definition vec2d.h:74
Vec2d Perpendicular() const
Computes perpendicular Vector.
Definition vec2d.h:61
void Add(Vec2d const &v)
Adds two vectors.
Definition vec2d.h:68
double NormSqr() const
Returns square of Euclidean distance from origin.
Definition vec2d.h:87
Vec2d operator/(double const &scalar) const
Definition vec2d.h:128
void DistTht(Vec2d const &v, double &d, double &tht) const
Computes distance and angle with another Vector (v-this)
Definition vec2d.h:110
double Dot(Vec2d const &v) const
Computes dot product of two Vectors.
Definition vec2d.h:84
Vec2d operator+(Vec2d const &vec) const
Definition vec2d.h:118
Vec2d operator-(Vec2d const &vec) const
Definition vec2d.h:122
double x() const
Definition vec2d.h:54
Vec2d(double const x_i, double const y_i)
Definition vec2d.h:52
double Norm() const
Returns Euclidean distance from origin.
Definition vec2d.h:90
double y() const
Definition vec2d.h:55
Vec2d operator*(double const &scalar) const
Definition vec2d.h:132
double DistSqr(Vec2d const &v) const
Gives the distance between the Vector and another Vector v.
Definition vec2d.h:100
double Dist(Vec2d const &v) const
Definition vec2d.h:107
void SetX(double x)
Definition vec2d.h:57
Vec2d operator-() const
Definition vec2d.h:126
int CosAngle(Vec2d const &v, double &ang) const
Gives cosine of the angle between this and Vector v.
Definition vec2d.h:93
void SetY(double y)
Definition vec2d.h:58
Constants for the CoverageControl library.
double const kEps
Definition constants.h:48
Namespace for the CoverageControl library.