Skip to content

Commit 7eee244

Browse files
authored
Merge pull request #17 from ichiro-its/15-add-direction-equation
Add Direction Equation
2 parents c47ceeb + bf47755 commit 7eee244

File tree

8 files changed

+71
-5
lines changed

8 files changed

+71
-5
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Keisan
22

3-
[![latest version](https://img.shields.io/github/v/release/ichiro-its/keisan.svg)](https://github.com/ichiro-its/keisan/releases/)
4-
[![license](https://img.shields.io/github/license/ichiro-its/keisan.svg)](./LICENSE)
5-
[![build and test status](https://github.com/ichiro-its/keisan/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/ichiro-its/keisan/actions)
3+
[![latest version](https://img.shields.io/github/v/release/ichiro-its/keisan)](https://github.com/ichiro-its/keisan/releases/)
4+
[![commits since latest version](https://img.shields.io/github/commits-since/ichiro-its/keisan/latest)](https://github.com/ichiro-its/keisan/commits/master)
5+
[![milestone](https://img.shields.io/github/milestones/progress/ichiro-its/keisan/1?label=milestone)](https://github.com/ichiro-its/keisan/milestone/1)
6+
[![license](https://img.shields.io/github/license/ichiro-its/keisan)](./LICENSE)
7+
[![test status](https://img.shields.io/github/workflow/status/ichiro-its/keisan/Build%20and%20Test?label=test)](https://github.com/ichiro-its/keisan/actions)
68

79
Keisan is a [ROS 2](https://docs.ros.org/en/foxy/index.html) package that provides a math equations and algorithms library for a ROS 2 project.
810

include/keisan/point_2.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ struct Point2
4848
Point2 operator/(double value);
4949

5050
static double distance_between(Point2 & point_a, Point2 & point_b);
51+
static double angle_between(Point2 & point_a, Point2 & point_b);
5152

5253
double magnitude();
54+
double direction();
55+
5356
Point2 normalize();
5457

5558
double x;

include/keisan/point_3.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ struct Point3
4848
Point3 operator/(double value);
4949

5050
static double distance_between(Point3 & point_a, Point3 & point_b);
51+
static double angle_between(Point3 & point_a, Point3 & point_b);
5152

5253
double magnitude();
54+
double direction();
55+
5356
Point3 normalize();
5457

5558
double x;

src/angle.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ double deg_to_rad(double value)
4646

4747
double delta_deg(double value1, double value2)
4848
{
49-
return wrap_deg(value2 - value1);
49+
return wrap_deg(wrap_deg(value2) - wrap_deg(value1));
5050
}
5151

5252
double delta_rad(double value1, double value2)
5353
{
54-
return wrap_rad(value2 - value1);
54+
return wrap_rad(wrap_rad(value2) - wrap_rad(value1));
5555
}
5656

5757
} // namespace keisan

src/point_2.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <math.h>
2222

23+
#include <keisan/angle.hpp>
2324
#include <keisan/point_2.hpp>
2425

2526
namespace keisan
@@ -137,11 +138,24 @@ double Point2::distance_between(Point2 & point_a, Point2 & point_b)
137138
return sqrt(dx * dx + dy * dy);
138139
}
139140

141+
double Point2::angle_between(Point2 & point_a, Point2 & point_b)
142+
{
143+
double dot_product = point_a.x * point_b.x + point_a.y * point_b.y;
144+
double mag = point_a.magnitude() * point_b.magnitude();
145+
146+
return wrap_rad(acos(dot_product / mag));
147+
}
148+
140149
double Point2::magnitude()
141150
{
142151
return sqrt(x * x + y * y);
143152
}
144153

154+
double Point2::direction()
155+
{
156+
return wrap_rad(atan(y / x));
157+
}
158+
145159
Point2 Point2::normalize()
146160
{
147161
double mag = magnitude();

src/point_3.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <math.h>
2222

23+
#include <keisan/angle.hpp>
2324
#include <keisan/point_3.hpp>
2425

2526
namespace keisan
@@ -148,11 +149,26 @@ double Point3::distance_between(Point3 & point_a, Point3 & point_b)
148149
return sqrt(dx * dx + dy * dy + dz * dz);
149150
}
150151

152+
double Point3::angle_between(Point3 & point_a, Point3 & point_b)
153+
{
154+
double dot_product = point_a.x * point_b.x + point_a.y * point_b.y + point_a.z * point_b.z;
155+
double mag = point_a.magnitude() * point_b.magnitude();
156+
157+
return wrap_rad(acos(dot_product / mag));
158+
}
159+
151160
double Point3::magnitude()
152161
{
153162
return sqrt(x * x + y * y + z * z);
154163
}
155164

165+
double Point3::direction()
166+
{
167+
double temp = sqrt(x * x + y * y);
168+
169+
return wrap_rad(atan(z / temp));
170+
}
171+
156172
Point3 Point3::normalize()
157173
{
158174
double mag = magnitude();

test/point_2_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,17 @@ TEST(Point2Test, Normalize)
119119
auto point = keisan::Point2(3.0, 4.0);
120120
ASSERT_POINT2_EQ(point.normalize(), 0.6, 0.8);
121121
}
122+
123+
TEST(Point2Test, Direction)
124+
{
125+
auto point = keisan::Point2(4.0, 0.0);
126+
ASSERT_DOUBLE_EQ(point.direction(), keisan::deg_to_rad(0.0));
127+
}
128+
129+
TEST(Point2Test, AngleBetween)
130+
{
131+
auto point_a = keisan::Point2(4.0, 3.0);
132+
auto point_b = keisan::Point2(-3.0, 4.0);
133+
134+
ASSERT_DOUBLE_EQ(keisan::Point2::angle_between(point_a, point_b), keisan::deg_to_rad(90.0));
135+
}

test/point_3_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,17 @@ TEST(Point3Test, Normalize)
119119
auto point = keisan::Point3(3.0, 0.0, 4.0);
120120
ASSERT_POINT3_EQ(point.normalize(), 0.6, 0.0, 0.8);
121121
}
122+
123+
TEST(Point3Test, Direction)
124+
{
125+
auto point = keisan::Point3(3.0, 4.0, 0.0);
126+
ASSERT_DOUBLE_EQ(point.direction(), keisan::deg_to_rad(0.0));
127+
}
128+
129+
TEST(Point3Test, AngleBetween)
130+
{
131+
auto point_a = keisan::Point3(2.0, -1.0, 7.0);
132+
auto point_b = keisan::Point3(1.0, 2.0, 0.0);
133+
134+
ASSERT_DOUBLE_EQ(keisan::Point3::angle_between(point_a, point_b), keisan::deg_to_rad(90.0));
135+
}

0 commit comments

Comments
 (0)