Commit aff5699d authored by VAN TOLL Wouter's avatar VAN TOLL Wouter
Browse files

Made Vector2D::x and Vector2D::y publicly accessible.

The methods x() and y() are no longer needed.
With the correct use of const, it should be impossible to affect vectors accidentally.
parent ee13f58e
......@@ -54,7 +54,7 @@ namespace ORCALibrary
{
inline float det(const Vector2D &vector1, const Vector2D &vector2)
{
return (float)(vector1.x() * vector2.y() - vector1.y() * vector2.x());
return (float)(vector1.x * vector2.y - vector1.y * vector2.x);
}
/* Search for the best new velocity. */
......@@ -129,7 +129,7 @@ namespace ORCALibrary
const float wLength = std::sqrt(wLengthSq);
const Vector2D& unitW = w / wLength;
line.direction = Vector2D(unitW.y(), -unitW.x());
line.direction = Vector2D(unitW.y, -unitW.x);
u = unitW * (combinedRadius / timeHorizon_ - wLength);
}
else {
......@@ -138,11 +138,11 @@ namespace ORCALibrary
if (det(relativePosition, w) > 0.0f) {
/* Project on left leg. */
line.direction = Vector2D(relativePosition.x() * leg - relativePosition.y() * combinedRadius, relativePosition.x() * combinedRadius + relativePosition.y() * leg) / distSq;
line.direction = Vector2D(relativePosition.x * leg - relativePosition.y * combinedRadius, relativePosition.x * combinedRadius + relativePosition.y * leg) / distSq;
}
else {
/* Project on right leg. */
line.direction = Vector2D(relativePosition.x() * leg + relativePosition.y() * combinedRadius, -relativePosition.x() * combinedRadius + relativePosition.y() * leg) * -1 / distSq;
line.direction = Vector2D(relativePosition.x * leg + relativePosition.y * combinedRadius, -relativePosition.x * combinedRadius + relativePosition.y * leg) * -1 / distSq;
}
const float dotProduct2 = relativeVelocity.dot(line.direction);
......@@ -159,7 +159,7 @@ namespace ORCALibrary
const float wLength = w.sqrMagnitude();
const Vector2D unitW = w / wLength;
line.direction = Vector2D(unitW.y(), -unitW.x());
line.direction = Vector2D(unitW.y, -unitW.x);
u = unitW * (combinedRadius / simulationTimeStep - wLength);
}
......@@ -309,7 +309,7 @@ namespace ORCALibrary
const Vector2D tempResult = result;
if (linearProgram2(projLines, radius, Vector2D(-lines[i].direction.y(), lines[i].direction.x()), true, result) < projLines.size()) {
if (linearProgram2(projLines, radius, Vector2D(-lines[i].direction.y, lines[i].direction.x), true, result) < projLines.size()) {
/* This should in principle not happen. The result is by definition
* already in the feasible region of this linear program. If it fails,
* it is due to small floating point error, and the current result is
......
......@@ -92,12 +92,12 @@ extern "C" {
for (int i = 0; i < (int)agents.size(); ++i)
{
agentData[i].id = (int)agents[i]->getID();
agentData[i].position_x = agents[i]->getPosition().x();
agentData[i].position_y = agents[i]->getPosition().y();
agentData[i].velocity_x = agents[i]->getVelocity().x();
agentData[i].velocity_y = agents[i]->getVelocity().y();
agentData[i].viewingDirection_x = agents[i]->getViewingDirection().x();
agentData[i].viewingDirection_y = agents[i]->getViewingDirection().y();
agentData[i].position_x = agents[i]->getPosition().x;
agentData[i].position_y = agents[i]->getPosition().y;
agentData[i].velocity_x = agents[i]->getVelocity().x;
agentData[i].velocity_y = agents[i]->getVelocity().y;
agentData[i].viewingDirection_x = agents[i]->getViewingDirection().x;
agentData[i].viewingDirection_y = agents[i]->getViewingDirection().y;
}
// store references to these results, for the client program to use
......
......@@ -53,8 +53,8 @@ private:
// "if/else's" are actually solved at compile time.
inline double kdtree_get_pt(const size_t idx, const size_t dim) const
{
if (dim == 0) return agentPositions[idx].second.x();
else return agentPositions[idx].second.y();
if (dim == 0) return agentPositions[idx].second.x;
else return agentPositions[idx].second.y;
}
// Optional bounding-box computation: return false to default to a standard bbox computation loop.
......
......@@ -51,7 +51,7 @@ public:
/// <param name="v1">A 2D vector containing the desired left column of the matrix.</param>
/// <param name="v2">A 2D vector containing the desired right column of the matrix.</param>
Matrix(const Vector2D& v1, const Vector2D& v2)
: a11_(v1.x()), a12_(v2.x()), a21_(v1.y()), a22_(v2.y()) {}
: a11_(v1.x), a12_(v2.x), a21_(v1.y), a22_(v2.y) {}
/// <summary>Computes and returns the transposed version of this Matrix.</summary>
/// <returns>A Matrix object similar to the current one, but with a21 and a12 swapped.</return>
......@@ -113,12 +113,12 @@ inline Matrix operator*(const Matrix& lhs, const float &rhs)
inline Vector2D operator*(const Matrix& lhs, const Vector2D& rhs)
{
return Vector2D(lhs.a11()*rhs.x() + lhs.a12()*rhs.y(), lhs.a21()*rhs.x() + lhs.a22()*rhs.y());
return Vector2D(lhs.a11()*rhs.x + lhs.a12()*rhs.y, lhs.a21()*rhs.x + lhs.a22()*rhs.y);
}
inline Matrix outerProduct(const Vector2D& lhs, const Vector2D& rhs)
{
return Matrix(lhs.x()*rhs.x(), lhs.x()*rhs.y(), lhs.y()*rhs.x(), lhs.y()*rhs.y());
return Matrix(lhs.x*rhs.x, lhs.x*rhs.y, lhs.y*rhs.x, lhs.y*rhs.y);
}
#endif // LIB_MATRIX_H
......@@ -30,32 +30,26 @@ const double PI = 3.1415926535897;
/// <summary>A 2D vector, used for representing positions, velocities, etc.</summary>
class Vector2D
{
private:
public:
/// <summary>The x component of this vector.</summary>
float x_;
float x;
/// <summary>The y component of this vector.</summary>
float y_;
float y;
public:
/// <summary>Creates a Vector2D with both components set to zero.</summary>
Vector2D() : x_(0.0f), y_(0.0f) {}
Vector2D() : x(0.0f), y(0.0f) {}
/// <summary>Creates a Vector2D with the given x and y components.</summary>
/// <param name="x">The desired x component of the vector.</param>
/// <param name="y">The desired y component of the vector.</param>
Vector2D(float x, float y) : x_(x), y_(y) { }
/// <summary>Retrieves the x component of this Vector2D.</summary>
inline float x() const { return x_; }
/// <summary>Retrieves the y component of this Vector2D.</summary>
inline float y() const { return y_; }
Vector2D(float x, float y) : x(x), y(y) { }
/// <summary>Computes and returns the magnitude of this Vector2D.</summary>
/// <returns>The vector's magnitude, i.e. sqrt(x*x + y*y).</returns>
inline float magnitude() const { return sqrtf(x_ * x_ + y_ * y_); }
inline float magnitude() const { return sqrtf(x * x + y * y); }
/// <summary>Computes and returns the squared magnitude of this Vector2D.</summary>
/// <returns>The vector's squared magnitude, i.e. x*x + y*y.</returns>
inline float sqrMagnitude() const { return x_ * x_ + y_ * y_; }
inline float sqrMagnitude() const { return x * x + y * y; }
/// <summary>Normalizes this Vector2D to unit length, by dividing both x and y by the magnitude.</summary>
inline void normalize()
......@@ -63,8 +57,8 @@ public:
float mag = magnitude();
if (mag > 0)
{
x_ /= mag;
y_ /= mag;
x /= mag;
y /= mag;
}
}
......@@ -72,7 +66,7 @@ public:
/// <returns>A Vector2D with the same direction as the current vector, but with unit length.</returns>
inline Vector2D getnormalized() const
{
Vector2D Result(x_, y_);
Vector2D Result(x, y);
Result.normalize();
return Result;
}
......@@ -82,16 +76,7 @@ public:
/// <returns>The dot product of the current Vector2D and 'other', i.e. x*other.x + y*other.y.</returns>
inline float dot(const Vector2D& other) const
{
return x_ * other.x_ + y_ * other.y_;
}
/// <summary>Sets the x and y components of this Vector2D to the given values.</summary>
/// <param name="x">The desired new x component of this vector.</param>
/// <param name="y">The desired new y component of this vector.</param>
void set(float x, float y)
{
x_ = x;
y_ = y;
return x * other.x + y * other.y;
}
/// <summary>Adds another Vector2D to the current Vector2D.</summary>
......@@ -99,7 +84,7 @@ public:
/// <returns>A reference to the current Vector2D after the operation has been performed.</returns>
inline Vector2D& operator+=(const Vector2D& rhs)
{
x_ += rhs.x(); y_ += rhs.y();
x += rhs.x; y += rhs.y;
return *this;
}
......@@ -108,7 +93,7 @@ public:
/// <returns>A reference to the current Vector2D after the operation has been performed.</returns>
inline Vector2D& operator-=(const Vector2D& rhs)
{
x_ -= rhs.x(); y_ -= rhs.y();
x -= rhs.x; y -= rhs.y;
return *this;
}
......@@ -116,7 +101,7 @@ public:
/// <returns>true if both x and y are 0; false otherwise.</returns>
inline bool isZero() const
{
return x_ == 0 && y_ == 0;
return x == 0 && y == 0;
}
};
......@@ -126,17 +111,17 @@ typedef std::pair<Vector2D, Vector2D> LineSegment2D;
inline Vector2D operator-(Vector2D lhs, Vector2D rhs)
{
return Vector2D(lhs.x() - rhs.x(), lhs.y() - rhs.y());
return Vector2D(lhs.x - rhs.x, lhs.y - rhs.y);
}
inline Vector2D operator+(Vector2D lhs, Vector2D rhs)
{
return Vector2D(lhs.x() + rhs.x(), lhs.y() + rhs.y());
return Vector2D(lhs.x + rhs.x, lhs.y + rhs.y);
}
inline Vector2D operator*(float lhs, Vector2D rhs)
{
return Vector2D(rhs.x() * lhs, rhs.y() * lhs);
return Vector2D(rhs.x * lhs, rhs.y * lhs);
}
inline Vector2D operator*(Vector2D lhs, float rhs)
......@@ -151,17 +136,17 @@ inline Vector2D operator/(Vector2D lhs, float rhs)
inline bool operator==(const Vector2D& p, const Vector2D& q)
{
return p.x() == q.x() && p.y() == q.y();
return p.x == q.x && p.y == q.y;
}
inline bool operator!=(const Vector2D& p, const Vector2D& q)
{
return p.x() != q.x() || p.y() != q.y();
return p.x != q.x || p.y != q.y;
}
inline Vector2D operator-(Vector2D lhs)
{
return Vector2D(-lhs.x(), -lhs.y());
return Vector2D(-lhs.x, -lhs.y);
}
#pragma endregion
......@@ -171,7 +156,7 @@ inline Vector2D operator-(Vector2D lhs)
inline Vector2D rotateCounterClockwise(const Vector2D& v, float radians)
{
const double Cos = cos(radians), Sin = sin(radians);
return Vector2D((float)(Cos*v.x() - Sin * v.y()), (float)(Sin*v.x() + Cos * v.y()));
return Vector2D((float)(Cos*v.x - Sin * v.y), (float)(Sin*v.x + Cos * v.y));
}
inline Vector2D rotateCounterClockwiseAroundPoint(const Vector2D& v, const Vector2D& pivot, float radians)
......@@ -205,13 +190,13 @@ inline float cosAngle(const Vector2D& va, const Vector2D& vb)
inline bool isClockwise(const Vector2D &vector1, const Vector2D &vector2)
{
float cross = vector1.x() * vector2.y() - vector1.y() * vector2.x();
float cross = vector1.x * vector2.y - vector1.y * vector2.x;
return cross < 0;
}
inline bool isCounterClockwise(const Vector2D &vector1, const Vector2D &vector2)
{
float cross = vector1.x() * vector2.y() - vector1.y() * vector2.x();
float cross = vector1.x * vector2.y - vector1.y * vector2.x;
return cross > 0;
}
......@@ -263,14 +248,14 @@ inline Vector2D clamp(const Vector2D& v, float maxLength)
inline bool getLineIntersection(const Vector2D& a, const Vector2D& b, const Vector2D& c, const Vector2D& d, Vector2D& result)
{
// Line AB represented as a1x + b1y = c1
double a1 = b.y() - a.y();
double b1 = a.x() - b.x();
double c1 = a1 * a.x() + b1 * a.y();
double a1 = b.y - a.y;
double b1 = a.x - b.x;
double c1 = a1 * a.x + b1 * a.y;
// Line CD represented as a2x + b2y = c2
double a2 = d.y() - c.y();
double b2 = c.x() - d.x();
double c2 = a2 * c.x() + b2 * c.y();
double a2 = d.y - c.y;
double b2 = c.x - d.x;
double c2 = a2 * c.x + b2 * c.y;
double determinant = a1 * b2 - a2 * b1;
......
......@@ -53,7 +53,7 @@ float FOEAvoidance::GetCost(const Vector2D& velocity, Agent* agent, const WorldB
const Vector2D& Position = agent->getPosition();
const Vector2D& Vnorm = velocity.getnormalized();
Matrix R(Vector2D(Vnorm.y(), -Vnorm.x()), Vnorm);
Matrix R(Vector2D(Vnorm.y, -Vnorm.x), Vnorm);
const Matrix& RT = R.GetTransposed();
float Cost = 0;
......@@ -65,15 +65,15 @@ float FOEAvoidance::GetCost(const Vector2D& velocity, Agent* agent, const WorldB
Vector2D Vel = RT * (neighbor.velocity - velocity);
Vector2D Pos = RT * (neighbor.position - Position);
if (Pos.y() < MIN_DISTANCE || Vel.y() > -MIN_VELOCITY)
if (Pos.y < MIN_DISTANCE || Vel.y > -MIN_VELOCITY)
continue;
float xf = Vel.x() / Vel.y();
float xg = Pos.x() / Pos.y();
float xf = Vel.x / Vel.y;
float xg = Pos.x / Pos.y;
float dx = xg - xf;
float ttc = -Pos.y() / Vel.y();
float ttc = -Pos.y / Vel.y;
float sigma = (float)(((agent->getRadius() + neighbor.realAgent->getRadius()) / Pos.y()) / log(10));
float sigma = (float)(((agent->getRadius() + neighbor.realAgent->getRadius()) / Pos.y) / log(10));
float localCost = Importance(ttc)*exp(-abs(dx) / sigma);
......@@ -93,7 +93,7 @@ Vector2D FOEAvoidance::GetGradient(const Vector2D& velocity, Agent* agent, const
const Vector2D& Position = agent->getPosition();
const Vector2D& Vnorm = velocity.getnormalized();
Matrix R(Vector2D(Vnorm.y(), -Vnorm.x()), Vnorm);
Matrix R(Vector2D(Vnorm.y, -Vnorm.x), Vnorm);
const Matrix& RT = R.GetTransposed();
float gradtheta = 0, gradv = 0;
......@@ -105,18 +105,18 @@ Vector2D FOEAvoidance::GetGradient(const Vector2D& velocity, Agent* agent, const
Vector2D Vel = RT * (neighbor.velocity - velocity);
Vector2D Pos = RT * (neighbor.position - Position);
if (Pos.y() < MIN_DISTANCE || Vel.y() > -MIN_VELOCITY)
if (Pos.y < MIN_DISTANCE || Vel.y > -MIN_VELOCITY)
continue;
float xf = Vel.x() / Vel.y();
float xg = Pos.x() / Pos.y();
float xf = Vel.x / Vel.y;
float xg = Pos.x / Pos.y;
float dx = xg - xf;
float ttc = -Pos.y() / Vel.y();
float ttc = -Pos.y / Vel.y;
float sigma = (float)(((agent->getRadius() + neighbor.realAgent->getRadius()) / Pos.y()) / log(10));
float sigma = (float)(((agent->getRadius() + neighbor.realAgent->getRadius()) / Pos.y) / log(10));
float localgradtheta = Importance(ttc)*exp(-abs(dx) / sigma) * sgn(dx)*(xf*xf - xg * xg - (velocity.magnitude() / (-Vel.y()))) / sigma;
float localgradv = Importance(ttc)*exp(-abs(dx) / sigma) * sgn(dx)*(-xf / (-Vel.y())) / sigma;
float localgradtheta = Importance(ttc)*exp(-abs(dx) / sigma) * sgn(dx)*(xf*xf - xg * xg - (velocity.magnitude() / (-Vel.y))) / sigma;
float localgradv = Importance(ttc)*exp(-abs(dx) / sigma) * sgn(dx)*(-xf / (-Vel.y)) / sigma;
gradtheta += localgradtheta;
gradv += localgradv;
......
......@@ -129,7 +129,7 @@ Vector2D TtcaDca::GetGradient(const Vector2D& velocity, Agent* agent, const Worl
const Vector2D& Position = agent->getPosition();
const float VelMagnitude = velocity.magnitude();
const Vector2D& VelNorm = velocity / VelMagnitude;
const Vector2D VelRot(velocity.y(), -velocity.x());
const Vector2D VelRot(velocity.y, -velocity.x);
// --- collision avoidance
......
......@@ -35,7 +35,7 @@ AgentKDTree::~AgentKDTree()
std::vector<size_t> AgentKDTree::FindAllAgentsInRange(const Vector2D& position, const float radius, const Agent* agentToIgnore) const
{
// do a radius search in the kd-tree
double q[2] = { position.x(), position.y() };
double q[2] = { position.x, position.y };
std::vector<std::pair<size_t, double>> result_indicesAndDistances;
nanoflann::SearchParams params; params.sorted = true;
// note: nanoflann uses squared distances, so we search with radius*radius
......@@ -67,7 +67,7 @@ std::vector<size_t> AgentKDTree::FindKNearestAgents(const Vector2D& position, co
++kSearch;
// do a knn search in the kd-tree
double q[2] = { position.x(), position.y() };
double q[2] = { position.x, position.y };
std::vector<size_t> result_indices(kSearch);
std::vector<double> result_distances(kSearch);
std::vector<std::pair<size_t, double>> result_indicesAndDistances;
......
......@@ -30,7 +30,7 @@ CostFunction::~CostFunction() {}
Vector2D CostFunction::RotateGradientToEuclideanCoordinates(const float GradTh, const float GradS, const Vector2D& direction, const float speed) const
{
Matrix R(Vector2D(direction.y(), -direction.x()), direction);
Matrix R(Vector2D(direction.y, -direction.x), direction);
return R * (Vector2D(sin(GradTh), 1 - cos(GradTh))*speed + Vector2D(0, GradS));
}
......
......@@ -57,12 +57,10 @@ bool CostFunctionParameters::ReadString(const std::string &name, std::string &va
bool CostFunctionParameters::ReadVector2D(const std::string &nameX, const std::string &nameY, Vector2D &value) const
{
float x, y;
if (!ReadFloat(nameX, x))
if (!ReadFloat(nameX, value.x))
return false;
if (!ReadFloat(nameY, y))
if (!ReadFloat(nameY, value.y))
return false;
value.set(x, y);
return true;
}
\ No newline at end of file
......@@ -48,16 +48,16 @@ NeighborList WorldToric::ComputeNeighbors(const Vector2D& position, float search
NeighborList result;
computeNeighbors_Displaced(position, Vector2D(0, 0), search_radius, queryingAgent, result);
if (position.x() - search_radius < -0.5*width_)
if (position.x - search_radius < -0.5*width_)
computeNeighbors_Displaced(position, Vector2D(width_, 0), search_radius, queryingAgent, result);
if (position.x() + search_radius > 0.5*width_)
if (position.x + search_radius > 0.5*width_)
computeNeighbors_Displaced(position, Vector2D(-width_, 0), search_radius, queryingAgent, result);
if (position.y() - search_radius < -0.5*height_)
if (position.y - search_radius < -0.5*height_)
computeNeighbors_Displaced(position, Vector2D(0, height_), search_radius, queryingAgent, result);
if (position.x() + search_radius > 0.5*height_)
if (position.x + search_radius > 0.5*height_)
computeNeighbors_Displaced(position, Vector2D(0, -height_), search_radius, queryingAgent, result);
return result;
......@@ -77,8 +77,8 @@ void WorldToric::DoStep_MoveAllAgents()
agent->UpdateVelocityAndPosition(delta_time_);
// if the agent has crossed the bounding rectangle, warp it to the other side
float x = agent->getPosition().x();
float y = agent->getPosition().y();
float x = agent->getPosition().x;
float y = agent->getPosition().y;
if (x > halfWidth)
x -= width_;
else if (x < -halfWidth)
......
......@@ -109,7 +109,7 @@ void CSVWriter::AppendAgentPositions(std::map<int, Vector2D> &poss, float t)
{
mtx_.lock();
for(auto itr=poss.begin(); itr != poss.end(); itr++)
pos_log_[itr->first].push_back(TimeAndPos(t, itr->second.x(), itr->second.y()));
pos_log_[itr->first].push_back(TimeAndPos(t, itr->second.x, itr->second.y));
mtx_.unlock();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment