Skip to content
This repository was archived by the owner on Mar 3, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3755394
Obstacle abstract class
PFGimenez May 20, 2018
c035764
Better using references… (I'm a noob)
PFGimenez May 20, 2018
614840b
Copy constructor for Vector2D
PFGimenez May 20, 2018
2b22ab5
CircularObstacle class
PFGimenez May 20, 2018
a932538
Add operator<< in CircularObstacle class
PFGimenez May 20, 2018
92144ae
Copy constructor is implicit
PFGimenez May 20, 2018
29222ec
Add override to circular_obstacle.h
PFGimenez May 20, 2018
d680994
Style
PFGimenez May 20, 2018
b6bf67e
no more pointers
kay0u May 20, 2018
2910d7f
Fix virtual destructor
May 21, 2018
3745205
operator==
PFGimenez May 23, 2018
f4a9cba
Check the typeid before static_cast
PFGimenez May 23, 2018
a51a910
Add constructor
May 23, 2018
c801c28
Implemente getExpandedConvexHull and squaredDistance
May 23, 2018
98a476b
Finish RectangularObstacle
kay0u May 23, 2018
faae850
fix Include
kay0u May 23, 2018
98a280e
add compoundObstacle
May 24, 2018
ba537cf
Use default values to simplify the construtors of RectangularObstacle
PFGimenez May 25, 2018
5f012c6
operator== for RectangularObstacle
PFGimenez May 25, 2018
be727fc
A bit better
PFGimenez May 25, 2018
e79c081
Compound_obstacle in kraken namespace and operator==
PFGimenez May 25, 2018
1bacac1
Remove superfluous kraken::
PFGimenez May 25, 2018
96de11d
add unit tests
May 29, 2018
5e8ca2b
constexpr and noexcept for vector_2d
kay0u Jun 12, 2018
259e1c4
constexpr and noexcept for Obstacle
kay0u Jun 12, 2018
9473903
constexpr, noexcept and auto for circular_obstacle
kay0u Jun 12, 2018
c5947a2
constexpr, noexcept and auto for rectangular_obstacle
kay0u Jun 12, 2018
5b6b18b
constexpr, noexcept and auto for compound_obstacle
kay0u Jun 12, 2018
a0cb062
Merge branch 'master' into obstacles
kay0u Jun 12, 2018
487966f
fix merge
kay0u Jun 12, 2018
4a1d35d
sqrt, sin and cos are not constexpr in each implementations
kay0u Jun 12, 2018
7177b01
Obstacle constructor is explicit
kay0u Jun 12, 2018
b3eaac2
non-default destructor for Obstacle
kay0u Jun 12, 2018
cb14459
non-default destructor for Obstacle
kay0u Jun 12, 2018
fc8598a
remove constexpr in Obstacle for gcc
kay0u Jun 12, 2018
e2e5602
fine! I'll not use constexpr!
kay0u Jun 12, 2018
9880df4
Add StaticObstacles
kay0u Jun 28, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions sources/obstacles/circular_obstacle.cpp
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_);
Copy link
Collaborator

@Hindi Hindi Jun 11, 2018

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)

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_;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be factorised? Something like:

return Obstacle::operator==(rhs) && typeid(*this) != typeid(rhs)
  && 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

}
37 changes: 37 additions & 0 deletions sources/obstacles/circular_obstacle.h
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
74 changes: 74 additions & 0 deletions sources/obstacles/compound_obstacle.cpp
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_;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as CircularObstacle::operator==(const Obstacle &rhs) const

}
}
29 changes: 29 additions & 0 deletions sources/obstacles/compound_obstacle.h
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
20 changes: 20 additions & 0 deletions sources/obstacles/obstacle.cpp
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_;
}
}
31 changes: 31 additions & 0 deletions sources/obstacles/obstacle.h
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
Loading