This repository was archived by the owner on Mar 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Obstacle and CircularObstacle #12
Open
PFGimenez
wants to merge
37
commits into
master
Choose a base branch
from
obstacles
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 23 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
3755394
Obstacle abstract class
PFGimenez c035764
Better using references… (I'm a noob)
PFGimenez 614840b
Copy constructor for Vector2D
PFGimenez 2b22ab5
CircularObstacle class
PFGimenez a932538
Add operator<< in CircularObstacle class
PFGimenez 92144ae
Copy constructor is implicit
PFGimenez 29222ec
Add override to circular_obstacle.h
PFGimenez d680994
Style
PFGimenez b6bf67e
no more pointers
kay0u 2910d7f
Fix virtual destructor
3745205
operator==
PFGimenez f4a9cba
Check the typeid before static_cast
PFGimenez a51a910
Add constructor
c801c28
Implemente getExpandedConvexHull and squaredDistance
98a476b
Finish RectangularObstacle
kay0u faae850
fix Include
kay0u 98a280e
add compoundObstacle
ba537cf
Use default values to simplify the construtors of RectangularObstacle
PFGimenez 5f012c6
operator== for RectangularObstacle
PFGimenez be727fc
A bit better
PFGimenez e79c081
Compound_obstacle in kraken namespace and operator==
PFGimenez 1bacac1
Remove superfluous kraken::
PFGimenez 96de11d
add unit tests
5e8ca2b
constexpr and noexcept for vector_2d
kay0u 259e1c4
constexpr and noexcept for Obstacle
kay0u 9473903
constexpr, noexcept and auto for circular_obstacle
kay0u c5947a2
constexpr, noexcept and auto for rectangular_obstacle
kay0u 5b6b18b
constexpr, noexcept and auto for compound_obstacle
kay0u a0cb062
Merge branch 'master' into obstacles
kay0u 487966f
fix merge
kay0u 4a1d35d
sqrt, sin and cos are not constexpr in each implementations
kay0u 7177b01
Obstacle constructor is explicit
kay0u b3eaac2
non-default destructor for Obstacle
kay0u cb14459
non-default destructor for Obstacle
kay0u fc8598a
remove constexpr in Obstacle for gcc
kay0u e2e5602
fine! I'll not use constexpr!
kay0u 9880df4
Add StaticObstacles
kay0u File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| #include "obstacle.h" | ||
| #include "circular_obstacle.h" | ||
| #include "rectangular_obstacle.h" | ||
| #include "../struct/vector_2d.h" | ||
| #include <cmath> | ||
| #include <typeinfo> | ||
|
|
||
| namespace kraken | ||
| { | ||
| CircularObstacle::CircularObstacle(const Vector2D &pos, float radius) : | ||
| Obstacle(pos), radius_(radius), squared_radius_(radius * radius) | ||
| { | ||
| } | ||
|
|
||
| bool CircularObstacle::isInObstacle(const Vector2D &pos) const | ||
| { | ||
| return pos.squaredDistance(rotation_center_) <= squared_radius_; | ||
| } | ||
|
|
||
| float CircularObstacle::squaredDistance(const Vector2D &pos) const | ||
| { | ||
| // TODO : is "distance" really necessary ? (it uses a std::sqrt computation) | ||
| float out = std::max(0.f, pos.distance(rotation_center_) - radius_); | ||
| return out * out; | ||
| } | ||
|
|
||
| bool CircularObstacle::isColliding(const Vector2D &point_a, const Vector2D &point_b) const | ||
| { | ||
| const Vector2D point_c = rotation_center_; | ||
| const Vector2D ab = point_b - point_a; | ||
| const Vector2D ac = point_c - point_a; | ||
| float numerator = std::abs(ab.getX() * ac.getY() - ab.getY() * ac.getX()); | ||
| float denominator = ab.squaredNorm(); | ||
| float distance = numerator * numerator / denominator; | ||
|
|
||
| // no collision with the line (AB) | ||
| if (distance > squared_radius_) | ||
| return false; | ||
|
|
||
| // collision with the segment [AB] | ||
| float pscal1 = ab.dot(ac); | ||
| float pscal2 = -ab.dot(point_c - point_b); | ||
| if (pscal1 >= 0 && pscal2 >= 0) | ||
| return true; | ||
|
|
||
| // check if A or B is in the circle | ||
| return isInObstacle(point_a) || isInObstacle(point_b); | ||
| } | ||
|
|
||
| void CircularObstacle::getExpandedConvexHull(const float &expansion, const float &longestAllowedLength, | ||
| std::vector<Vector2D> &vector_2d_list) const | ||
| { | ||
| int nbPoints = std::ceil(M_2_PI * (radius_ + expansion) / longestAllowedLength); | ||
| if (nbPoints < 3) | ||
| nbPoints = 3; | ||
|
|
||
| for (int i = 0; i < nbPoints; ++i) | ||
| vector_2d_list.push_back( | ||
| Vector2D::fromPolar(expansion + radius_, i * M_2_PI / nbPoints) + rotation_center_); | ||
| } | ||
|
|
||
| bool CircularObstacle::operator==(const Obstacle &rhs) const | ||
| { | ||
| if (!Obstacle::operator==(rhs)) | ||
| return false; | ||
|
|
||
| if (typeid(*this) != typeid(rhs)) | ||
| return false; | ||
|
|
||
| return radius_ == static_cast<const CircularObstacle &>(rhs).radius_; | ||
|
||
| } | ||
|
|
||
| bool CircularObstacle::isColliding(const RectangularObstacle &obs) const | ||
| { | ||
| if (rotation_center_.squaredDistance(obs.getRotationCenter()) >= | ||
| (radius_ + obs.getHalfDiagonal()) * (radius_ + obs.getHalfDiagonal())) | ||
| return false; | ||
| return obs.squaredDistance(rotation_center_) < squared_radius_; | ||
| } | ||
|
|
||
| #if DEBUG | ||
|
|
||
| std::ostream &operator<<(std::ostream &strm, const CircularObstacle &o) | ||
| { | ||
| return strm << "CircularObstacle(" << o.rotation_center_ << "," << o.radius_ << ")" << std::endl; | ||
| } | ||
|
|
||
| #endif | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #ifndef TESTS_CIRCULAR_OBSTACLE_H | ||
| #define TESTS_CIRCULAR_OBSTACLE_H | ||
|
|
||
| #include "../struct/vector_2d.h" | ||
| #include "obstacle.h" | ||
|
|
||
| #if DEBUG | ||
|
|
||
| #include <ostream> | ||
|
|
||
| #endif | ||
|
|
||
| namespace kraken | ||
| { | ||
| class CircularObstacle : public Obstacle | ||
| { | ||
| public: | ||
| CircularObstacle(const Vector2D &pos, float radius); | ||
| bool isInObstacle(const Vector2D &pos) const override; | ||
| float squaredDistance(const Vector2D &pos) const override; | ||
| void getExpandedConvexHull(const float &expansion, const float &longestAllowedLength, | ||
| std::vector<Vector2D> &vector_2d_list) const override; | ||
| bool isColliding(const Vector2D &point_a, const Vector2D &point_b) const override; | ||
| bool isColliding(const RectangularObstacle &obs) const override; | ||
| bool operator==(const Obstacle &rhs) const override; | ||
|
|
||
| #if DEBUG | ||
| friend std::ostream &operator<<(std::ostream &strm, const CircularObstacle &o); | ||
| #endif | ||
|
|
||
| protected: | ||
| float const radius_; | ||
| float const squared_radius_; | ||
| }; | ||
| } | ||
|
|
||
| #endif //TESTS_CIRCULAR_OBSTACLE_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| #include "compound_obstacle.h" | ||
|
|
||
| #include <limits> | ||
| #include <typeinfo> | ||
|
|
||
| namespace kraken | ||
| { | ||
| CompoundObstacle::CompoundObstacle(const Vector2D &rotation_center, | ||
| std::vector<Obstacle> obstacles_list) : | ||
| Obstacle(rotation_center), obstacles_list_(std::move(obstacles_list)) | ||
| { | ||
| } | ||
|
|
||
| bool CompoundObstacle::isInObstacle(const Vector2D &pos) const | ||
| { | ||
| for (auto const &o : obstacles_list_) | ||
| { | ||
| if (o.isInObstacle(pos)) | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| float CompoundObstacle::squaredDistance(const Vector2D &pos) const | ||
| { | ||
| float min = std::numeric_limits<float>::max(); | ||
| for (auto const &o : obstacles_list_) | ||
| { | ||
| min = std::min(min, o.squaredDistance(pos)); | ||
| if (min == 0) | ||
| break; | ||
| } | ||
| return min; | ||
| } | ||
|
|
||
| void CompoundObstacle::getExpandedConvexHull(const float &expansion, const float &longestAllowedLength, | ||
| std::vector<Vector2D> &vector_2d_list) const | ||
| { | ||
| for (const auto &o : obstacles_list_) | ||
| o.getExpandedConvexHull(expansion, longestAllowedLength, vector_2d_list); | ||
| } | ||
|
|
||
| bool CompoundObstacle::isColliding(const Vector2D &point_a, const Vector2D &point_b) const | ||
| { | ||
| for (auto const &o : obstacles_list_) | ||
| { | ||
| if (o.isColliding(point_a, point_b)) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| bool CompoundObstacle::isColliding(const RectangularObstacle &obs) const | ||
| { | ||
| for (auto const &o : obstacles_list_) | ||
| { | ||
| if (o.isColliding(obs)) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| bool CompoundObstacle::operator==(const Obstacle &rhs) const | ||
| { | ||
| if (!Obstacle::operator==(rhs)) | ||
| return false; | ||
|
|
||
| if (typeid(*this) != typeid(rhs)) | ||
| return false; | ||
|
|
||
| return static_cast<const CompoundObstacle &>(rhs).obstacles_list_ == obstacles_list_; | ||
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #ifndef TESTS_COMPOUND_OBSTACLE_H | ||
| #define TESTS_COMPOUND_OBSTACLE_H | ||
|
|
||
| #include <vector> | ||
| #include "obstacle.h" | ||
|
|
||
| namespace kraken | ||
| { | ||
|
|
||
| class CompoundObstacle : public Obstacle | ||
| { | ||
| public: | ||
| CompoundObstacle(const Vector2D &rotation_center, std::vector<Obstacle> obstacles_list); | ||
|
|
||
| bool isInObstacle(const Vector2D &pos) const override; | ||
| float squaredDistance(const Vector2D &pos) const override; | ||
| void getExpandedConvexHull(const float &expansion, const float &longestAllowedLength, | ||
| std::vector<Vector2D> &vector_2d_list) const override; | ||
| bool isColliding(const Vector2D &point_a, const Vector2D &point_b) const override; | ||
| bool isColliding(const RectangularObstacle &obs) const override; | ||
|
|
||
| bool operator==(const Obstacle &rhs) const override; | ||
|
|
||
| protected: | ||
| const std::vector<Obstacle> obstacles_list_; | ||
| }; | ||
|
|
||
| } | ||
| #endif //TESTS_COMPOUND_OBSTACLE_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #include "obstacle.h" | ||
|
|
||
|
|
||
| namespace kraken | ||
| { | ||
|
|
||
| Obstacle::Obstacle(const Vector2D &rotation_center) : rotation_center_(rotation_center) | ||
| { | ||
| } | ||
|
|
||
| bool Obstacle::operator==(const Obstacle &rhs) const | ||
| { | ||
| return rotation_center_ == rhs.rotation_center_; | ||
| } | ||
|
|
||
| const Vector2D &Obstacle::getRotationCenter() const | ||
| { | ||
| return rotation_center_; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #ifndef TESTS_OBSTACLE_H | ||
| #define TESTS_OBSTACLE_H | ||
|
|
||
| #include <vector> | ||
| #include "../struct/vector_2d.h" | ||
|
|
||
| namespace kraken | ||
| { | ||
| class RectangularObstacle; | ||
|
|
||
| class Obstacle | ||
| { | ||
| public: | ||
| Obstacle(const Vector2D &rotation_center); | ||
| virtual ~Obstacle() = default; | ||
| virtual bool isInObstacle(const Vector2D &pos) const = 0; | ||
| virtual float squaredDistance(const Vector2D &pos) const = 0; | ||
| virtual void getExpandedConvexHull(const float &expansion, const float &longestAllowedLength, | ||
| std::vector<Vector2D> &vector_2d_list) const = 0; | ||
| virtual bool isColliding(const Vector2D &point_a, const Vector2D &point_b) const = 0; | ||
| virtual bool isColliding(const RectangularObstacle &obs) const = 0; | ||
| virtual bool operator==(const Obstacle &rhs) const; | ||
|
|
||
| const Vector2D &getRotationCenter() const; | ||
| protected: | ||
| Vector2D rotation_center_; | ||
|
|
||
| }; | ||
| } | ||
|
|
||
| #endif //TESTS_OBSTACLE_H |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should consider implementing an approximation of the euclidean distance that does not uses sqrt and add an entry in the config for this. (this has nothing to do with the merging request)