From ad0138713bc64ba1f647a06b1bdf50d8a4912461 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 25 Apr 2021 16:59:58 +0700 Subject: [PATCH 1/3] feat: add const in points --- include/keisan/point_2.hpp | 32 ++++++++++++++++---------------- include/keisan/point_3.hpp | 32 ++++++++++++++++---------------- src/point_2.cpp | 32 ++++++++++++++++---------------- src/point_3.cpp | 32 ++++++++++++++++---------------- 4 files changed, 64 insertions(+), 64 deletions(-) diff --git a/include/keisan/point_2.hpp b/include/keisan/point_2.hpp index 2d989f9..419f78b 100644 --- a/include/keisan/point_2.hpp +++ b/include/keisan/point_2.hpp @@ -30,32 +30,32 @@ struct Point2 Point2(double x, double y); Point2(const Point2 & point); - Point2 & operator=(Point2 & point); - Point2 & operator+=(Point2 & point); - Point2 & operator-=(Point2 & point); + Point2 & operator=(const Point2 & point); + Point2 & operator+=(const Point2 & point); + Point2 & operator-=(const Point2 & point); Point2 & operator+=(double value); Point2 & operator-=(double value); Point2 & operator*=(double value); Point2 & operator/=(double value); - Point2 operator+(Point2 & point); - Point2 operator-(Point2 & point); + Point2 operator+(const Point2 & point) const; + Point2 operator-(const Point2 & point) const; - Point2 operator+(double value); - Point2 operator-(double value); - Point2 operator*(double value); - Point2 operator/(double value); + Point2 operator+(double value) const; + Point2 operator-(double value) const; + Point2 operator*(double value) const; + Point2 operator/(double value) const; - static double distance_between(Point2 & point_a, Point2 & point_b); - static double angle_between(Point2 & point_a, Point2 & point_b); - static double dot_product(Point2 & point_a, Point2 & point_b); - static double cross_product(Point2 & point_a, Point2 & point_b); + static double distance_between(const Point2 & point_a, const Point2 & point_b); + static double angle_between(const Point2 & point_a, const Point2 & point_b); + static double dot_product(const Point2 & point_a, const Point2 & point_b); + static double cross_product(const Point2 & point_a, const Point2 & point_b); - double magnitude(); - double direction(); + double magnitude() const; + double direction() const; - Point2 normalize(); + Point2 normalize() const; double x; double y; diff --git a/include/keisan/point_3.hpp b/include/keisan/point_3.hpp index 579bc0e..8dd42fe 100644 --- a/include/keisan/point_3.hpp +++ b/include/keisan/point_3.hpp @@ -30,32 +30,32 @@ struct Point3 Point3(double x, double y, double z); Point3(const Point3 & point); - Point3 & operator=(Point3 & point); - Point3 & operator+=(Point3 & point); - Point3 & operator-=(Point3 & point); + Point3 & operator=(const Point3 & point); + Point3 & operator+=(const Point3 & point); + Point3 & operator-=(const Point3 & point); Point3 & operator+=(double value); Point3 & operator-=(double value); Point3 & operator*=(double value); Point3 & operator/=(double value); - Point3 operator+(Point3 & point); - Point3 operator-(Point3 & point); + Point3 operator+(const Point3 & point) const; + Point3 operator-(const Point3 & point) const; - Point3 operator+(double value); - Point3 operator-(double value); - Point3 operator*(double value); - Point3 operator/(double value); + Point3 operator+(double value) const; + Point3 operator-(double value) const; + Point3 operator*(double value) const; + Point3 operator/(double value) const; - static double distance_between(Point3 & point_a, Point3 & point_b); - static double angle_between(Point3 & point_a, Point3 & point_b); - static double dot_product(Point3 & point_a, Point3 & point_b); - static double cross_product(Point3 & point_a, Point3 & point_b); + static double distance_between(const Point3 & point_a, const Point3 & point_b); + static double angle_between(const Point3 & point_a, const Point3 & point_b); + static double dot_product(const Point3 & point_a, const Point3 & point_b); + static double cross_product(const Point3 & point_a, const Point3 & point_b); - double magnitude(); - double direction(); + double magnitude() const; + double direction() const; - Point3 normalize(); + Point3 normalize() const; double x; double y; diff --git a/src/point_2.cpp b/src/point_2.cpp index b418a9b..e364917 100644 --- a/src/point_2.cpp +++ b/src/point_2.cpp @@ -44,7 +44,7 @@ Point2::Point2(const Point2 & point) y = point.y; } -Point2 & Point2::operator=(Point2 & point) +Point2 & Point2::operator=(const Point2 & point) { x = point.x; y = point.y; @@ -52,7 +52,7 @@ Point2 & Point2::operator=(Point2 & point) return *this; } -Point2 & Point2::operator+=(Point2 & point) +Point2 & Point2::operator+=(const Point2 & point) { x += point.x; y += point.y; @@ -60,7 +60,7 @@ Point2 & Point2::operator+=(Point2 & point) return *this; } -Point2 & Point2::operator-=(Point2 & point) +Point2 & Point2::operator-=(const Point2 & point) { x -= point.x; y -= point.y; @@ -100,37 +100,37 @@ Point2 & Point2::operator/=(double value) return *this; } -Point2 Point2::operator+(Point2 & point) +Point2 Point2::operator+(const Point2 & point) const { return Point2(x + point.x, y + point.y); } -Point2 Point2::operator-(Point2 & point) +Point2 Point2::operator-(const Point2 & point) const { return Point2(x - point.x, y - point.y); } -Point2 Point2::operator+(double value) +Point2 Point2::operator+(double value) const { return Point2(x + value, y + value); } -Point2 Point2::operator-(double value) +Point2 Point2::operator-(double value) const { return Point2(x - value, y - value); } -Point2 Point2::operator*(double value) +Point2 Point2::operator*(double value) const { return Point2(x * value, y * value); } -Point2 Point2::operator/(double value) +Point2 Point2::operator/(double value) const { return Point2(x / value, y / value); } -double Point2::distance_between(Point2 & point_a, Point2 & point_b) +double Point2::distance_between(const Point2 & point_a, const Point2 & point_b) { double dx = point_a.x - point_b.x; double dy = point_a.y - point_b.y; @@ -138,7 +138,7 @@ double Point2::distance_between(Point2 & point_a, Point2 & point_b) return sqrt(dx * dx + dy * dy); } -double Point2::angle_between(Point2 & point_a, Point2 & point_b) +double Point2::angle_between(const Point2 & point_a, const Point2 & point_b) { double dot = dot_product(point_a, point_b); double mag = point_a.magnitude() * point_b.magnitude(); @@ -146,27 +146,27 @@ double Point2::angle_between(Point2 & point_a, Point2 & point_b) return wrap_rad(acos(dot / mag)); } -double Point2::dot_product(Point2 & point_a, Point2 & point_b) +double Point2::dot_product(const Point2 & point_a, const Point2 & point_b) { return point_a.x * point_b.x + point_a.y * point_b.y; } -double Point2::cross_product(Point2 & point_a, Point2 & point_b) +double Point2::cross_product(const Point2 & point_a, const Point2 & point_b) { return point_a.magnitude() * point_b.magnitude() * sin(angle_between(point_a, point_b)); } -double Point2::magnitude() +double Point2::magnitude() const { return sqrt(x * x + y * y); } -double Point2::direction() +double Point2::direction() const { return wrap_rad(atan(y / x)); } -Point2 Point2::normalize() +Point2 Point2::normalize() const { double mag = magnitude(); diff --git a/src/point_3.cpp b/src/point_3.cpp index 780bac2..f0df818 100644 --- a/src/point_3.cpp +++ b/src/point_3.cpp @@ -47,7 +47,7 @@ Point3::Point3(const Point3 & point) z = point.z; } -Point3 & Point3::operator=(Point3 & point) +Point3 & Point3::operator=(const Point3 & point) { x = point.x; y = point.y; @@ -56,7 +56,7 @@ Point3 & Point3::operator=(Point3 & point) return *this; } -Point3 & Point3::operator+=(Point3 & point) +Point3 & Point3::operator+=(const Point3 & point) { x += point.x; y += point.y; @@ -65,7 +65,7 @@ Point3 & Point3::operator+=(Point3 & point) return *this; } -Point3 & Point3::operator-=(Point3 & point) +Point3 & Point3::operator-=(const Point3 & point) { x -= point.x; y -= point.y; @@ -110,37 +110,37 @@ Point3 & Point3::operator/=(double value) return *this; } -Point3 Point3::operator+(Point3 & point) +Point3 Point3::operator+(const Point3 & point) const { return Point3(x + point.x, y + point.y, z + point.z); } -Point3 Point3::operator-(Point3 & point) +Point3 Point3::operator-(const Point3 & point) const { return Point3(x - point.x, y - point.y, z - point.z); } -Point3 Point3::operator+(double value) +Point3 Point3::operator+(double value) const { return Point3(x + value, y + value, z + value); } -Point3 Point3::operator-(double value) +Point3 Point3::operator-(double value) const { return Point3(x - value, y - value, z - value); } -Point3 Point3::operator*(double value) +Point3 Point3::operator*(double value) const { return Point3(x * value, y * value, z * value); } -Point3 Point3::operator/(double value) +Point3 Point3::operator/(double value) const { return Point3(x / value, y / value, z / value); } -double Point3::distance_between(Point3 & point_a, Point3 & point_b) +double Point3::distance_between(const Point3 & point_a, const Point3 & point_b) { double dx = point_a.x - point_b.x; double dy = point_a.y - point_b.y; @@ -149,7 +149,7 @@ double Point3::distance_between(Point3 & point_a, Point3 & point_b) return sqrt(dx * dx + dy * dy + dz * dz); } -double Point3::angle_between(Point3 & point_a, Point3 & point_b) +double Point3::angle_between(const Point3 & point_a, const Point3 & point_b) { double dot = dot_product(point_a, point_b); double mag = point_a.magnitude() * point_b.magnitude(); @@ -157,29 +157,29 @@ double Point3::angle_between(Point3 & point_a, Point3 & point_b) return wrap_rad(acos(dot / mag)); } -double Point3::dot_product(Point3 & point_a, Point3 & point_b) +double Point3::dot_product(const Point3 & point_a, const Point3 & point_b) { return point_a.x * point_b.x + point_a.y * point_b.y + point_a.z * point_b.z; } -double Point3::cross_product(Point3 & point_a, Point3 & point_b) +double Point3::cross_product(const Point3 & point_a, const Point3 & point_b) { return point_a.magnitude() * point_b.magnitude() * sin(angle_between(point_a, point_b)); } -double Point3::magnitude() +double Point3::magnitude() const { return sqrt(x * x + y * y + z * z); } -double Point3::direction() +double Point3::direction() const { double temp = sqrt(x * x + y * y); return wrap_rad(atan(z / temp)); } -Point3 Point3::normalize() +Point3 Point3::normalize() const { double mag = magnitude(); From 1d4e2364f50a5e903690512788e9880e46bbf782 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 25 Apr 2021 17:03:42 +0700 Subject: [PATCH 2/3] fix: replace atan with atan2 in direction --- src/point_2.cpp | 2 +- test/point_2_test.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/point_2.cpp b/src/point_2.cpp index e364917..8047690 100644 --- a/src/point_2.cpp +++ b/src/point_2.cpp @@ -163,7 +163,7 @@ double Point2::magnitude() const double Point2::direction() const { - return wrap_rad(atan(y / x)); + return wrap_rad(atan2(y, x)); } Point2 Point2::normalize() const diff --git a/test/point_2_test.cpp b/test/point_2_test.cpp index 306d724..d48291e 100644 --- a/test/point_2_test.cpp +++ b/test/point_2_test.cpp @@ -122,8 +122,8 @@ TEST(Point2Test, Normalize) TEST(Point2Test, Direction) { - auto point = keisan::Point2(4.0, 0.0); - ASSERT_DOUBLE_EQ(point.direction(), keisan::deg_to_rad(0.0)); + ASSERT_DOUBLE_EQ(keisan::Point2(0.0, 4.0).direction(), keisan::deg_to_rad(90.0)); + ASSERT_DOUBLE_EQ(keisan::Point2(-4.0, 0.0).direction(), keisan::deg_to_rad(180.0)); } TEST(Point2Test, AngleBetween) From 99bf979d2d56f86cc63f76e17c25fb94b75c79bf Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 25 Apr 2021 17:06:32 +0700 Subject: [PATCH 3/3] docs: update information and copyright --- CMakeLists.txt | 1 + LICENSE | 2 +- README.md | 2 +- include/keisan/angle.hpp | 2 +- include/keisan/keisan.hpp | 2 +- include/keisan/number.hpp | 2 +- include/keisan/point_2.hpp | 2 +- include/keisan/point_3.hpp | 2 +- package.xml | 4 ++-- src/angle.cpp | 2 +- src/number.cpp | 2 +- src/point_2.cpp | 2 +- src/point_3.cpp | 2 +- test/angle_test.cpp | 2 +- test/number_test.cpp | 2 +- test/point_2_test.cpp | 2 +- test/point_3_test.cpp | 2 +- 17 files changed, 18 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ece18b9..7984534 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,4 +55,5 @@ endif() ament_export_include_directories("include") ament_export_libraries(${PROJECT_NAME}) + ament_package() diff --git a/LICENSE b/LICENSE index 2ae239c..2a819b1 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 Ichiro ITS +Copyright (c) 2021 ICHIRO ITS Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 4faf5cd..4e5695d 100644 --- a/README.md +++ b/README.md @@ -10,4 +10,4 @@ Keisan is a [ROS 2](https://docs.ros.org/en/foxy/index.html) package that provid ## License -This project is maintained by [ICHIRO ITS](https://github.com/ichiro-its) and licensed under [the MIT License](./LICENSE). +This project is maintained by [ICHIRO ITS](https://github.com/ichiro-its) and licensed under the [MIT License](./LICENSE). diff --git a/include/keisan/angle.hpp b/include/keisan/angle.hpp index 6ef01d8..8500fc5 100644 --- a/include/keisan/angle.hpp +++ b/include/keisan/angle.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Ichiro ITS +// Copyright (c) 2021 ICHIRO ITS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/include/keisan/keisan.hpp b/include/keisan/keisan.hpp index 2c39518..b1af831 100644 --- a/include/keisan/keisan.hpp +++ b/include/keisan/keisan.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Ichiro ITS +// Copyright (c) 2021 ICHIRO ITS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/include/keisan/number.hpp b/include/keisan/number.hpp index 40f5db3..308fdba 100644 --- a/include/keisan/number.hpp +++ b/include/keisan/number.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Ichiro ITS +// Copyright (c) 2021 ICHIRO ITS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/include/keisan/point_2.hpp b/include/keisan/point_2.hpp index 419f78b..6e0a33c 100644 --- a/include/keisan/point_2.hpp +++ b/include/keisan/point_2.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Ichiro ITS +// Copyright (c) 2021 ICHIRO ITS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/include/keisan/point_3.hpp b/include/keisan/point_3.hpp index 8dd42fe..9172779 100644 --- a/include/keisan/point_3.hpp +++ b/include/keisan/point_3.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Ichiro ITS +// Copyright (c) 2021 ICHIRO ITS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/package.xml b/package.xml index 57e48fb..db64be3 100644 --- a/package.xml +++ b/package.xml @@ -2,8 +2,8 @@ keisan - 0.1.0 - A Math equations and algorithms package + 0.2.0 + Math equations and algorithms library Alfi Maulana MIT License ament_cmake diff --git a/src/angle.cpp b/src/angle.cpp index aae48a5..51846ef 100644 --- a/src/angle.cpp +++ b/src/angle.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Ichiro ITS +// Copyright (c) 2021 ICHIRO ITS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/src/number.cpp b/src/number.cpp index 9dfbdc1..ebf4f97 100644 --- a/src/number.cpp +++ b/src/number.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Ichiro ITS +// Copyright (c) 2021 ICHIRO ITS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/src/point_2.cpp b/src/point_2.cpp index 8047690..bbbd8cc 100644 --- a/src/point_2.cpp +++ b/src/point_2.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Ichiro ITS +// Copyright (c) 2021 ICHIRO ITS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/src/point_3.cpp b/src/point_3.cpp index f0df818..79a95a7 100644 --- a/src/point_3.cpp +++ b/src/point_3.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Ichiro ITS +// Copyright (c) 2021 ICHIRO ITS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/test/angle_test.cpp b/test/angle_test.cpp index 31e6354..9ed37a7 100644 --- a/test/angle_test.cpp +++ b/test/angle_test.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Ichiro ITS +// Copyright (c) 2021 ICHIRO ITS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/test/number_test.cpp b/test/number_test.cpp index 16dec14..ee29716 100644 --- a/test/number_test.cpp +++ b/test/number_test.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Ichiro ITS +// Copyright (c) 2021 ICHIRO ITS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/test/point_2_test.cpp b/test/point_2_test.cpp index d48291e..996b95d 100644 --- a/test/point_2_test.cpp +++ b/test/point_2_test.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Ichiro ITS +// Copyright (c) 2021 ICHIRO ITS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/test/point_3_test.cpp b/test/point_3_test.cpp index 48c72c8..53b6bf3 100644 --- a/test/point_3_test.cpp +++ b/test/point_3_test.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021 Ichiro ITS +// Copyright (c) 2021 ICHIRO ITS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal