Commit 05ab8c83 by VAN TOLL Wouter

### Matrix: Added documentation, cleaned up the class, and fixed bugs in two (unused) methods.

parent 723d47fd
 ... ... @@ -27,61 +27,100 @@ #include #include class Matrix { private: float a11_; float a12_; float a21_; float a22_; public: Matrix() { a11_ = 0.0; a12_ = 0.0; a21_ = 0.0; a22_ = 0.0; } Matrix(float a11, float a12, float a21, float a22) { a11_ = a11; a12_ = a12; a21_ = a21; a22_ = a22; } Matrix(Matrix * m) { this->a11_ = m->a11_; this->a12_ = m->a12_; this->a21_ = m->a21_; this->a22_ = m->a22_; } Matrix(Vector2D v1, Vector2D v2) { a11_ = v1.x(); a21_ = v1.y(); a12_ = v2.x(); a22_ = v2.y(); } Matrix transpose(); float determinant(); float a11() const { return a11_; } float a12() const { return a12_; } float a21() const { return a21_; } float a22() const { return a22_; } /// A 2x2 rotation matrix. Used by certain navigation functions. class Matrix { private: float a11_; float a12_; float a21_; float a22_; public: /// Creates a Matrix object with all values set to zero. Matrix() : a11_(0.0f), a12_(0.0f), a21_(0.0f), a22_(0.0f) {} /// Creates a Matrix object with the given values. /// The desired top-left value of the matrix. /// The desired top-right value of the matrix. /// The desired bottom-left value of the matrix. /// The desired bottom-right value of the matrix. Matrix(float a11, float a12, float a21, float a22) : a11_(a11), a12_(a12), a21_(a21), a22_(a22) {} /// Creates a Matrix object with its two columns filled in with the values from two given vectors. /// A 2D vector containing the desired left column of the matrix. /// A 2D vector containing the desired right column of the matrix. Matrix(const Vector2D& v1, const Vector2D& v2) : a11_(v1.x()), a12_(v2.x()), a21_(v1.y()), a22_(v2.y()) {} /// Computes and returns the transposed version of this Matrix. /// A Matrix object similar to the current one, but with a21 and a12 swapped. inline Matrix GetTransposed() const { return Matrix(a11_, a21_, a12_, a22_); } /// Computes and returns the determinant of this Matrix. /// The determinant of this matrix: a11 * a22 - a21 * a12. inline float Determinant() const { return a11_ * a22_ - a21_ * a12_; } /// Returns the top-left component of this Matrix. inline float a11() const { return a11_; } /// Returns the top-right component of this Matrix. inline float a12() const { return a12_; } /// Returns the bottom-left component of this Matrix. inline float a21() const { return a21_; } /// Returns the bottom-right component of this Matrix. inline float a22() const { return a22_; } }; //Add Matrix product operators Matrix operator+(const Matrix& lhs, const Matrix &rhs); Matrix operator-(const Matrix& lhs, const Matrix &rhs); Matrix operator*(const Matrix& lhs, const Matrix &rhs); Matrix operator*(const float& lhs, const Matrix &rhs); Matrix operator*(const Matrix& lhs, const float &rhs); Vector2D operator*(const Matrix& lhs, const Vector2D &rhs); Matrix outerProduct(const Vector2D& lhs, const Vector2D& rhs); inline Matrix operator+(const Matrix& lhs, const Matrix& rhs) { return Matrix(lhs.a11() + rhs.a11(), lhs.a12() + rhs.a12(), lhs.a21() + rhs.a21(), lhs.a22() + rhs.a22()); } inline Matrix operator-(const Matrix& lhs, const Matrix& rhs) { return Matrix(lhs.a11() - rhs.a11(), lhs.a12() - rhs.a12(), lhs.a21() - rhs.a21(), lhs.a22() - rhs.a22()); } inline Matrix operator*(const Matrix& lhs, const Matrix& rhs) { return Matrix(lhs.a11()*rhs.a11() + lhs.a12() * rhs.a21(), lhs.a11()*rhs.a12() + lhs.a12() * rhs.a22(), lhs.a21()*rhs.a11() + lhs.a22() * rhs.a21(), lhs.a21()*rhs.a12() + lhs.a22() * rhs.a22()); } inline Matrix operator*(const float& lhs, const Matrix& rhs) { return Matrix(lhs*rhs.a11(), lhs*rhs.a12(), lhs*rhs.a21(), lhs*rhs.a22()); } inline Matrix operator*(const Matrix& lhs, const float &rhs) { return rhs * lhs; } 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()); } 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()); } #endif // LIB_MATRIX_H
 ... ... @@ -56,7 +56,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); const Matrix& RT = R.transpose(); const Matrix& RT = R.GetTransposed(); float Cost = 0; const auto& neighbors = agent->getNeighborSubsetInRange(range_); ... ... @@ -91,7 +91,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); const Matrix& RT = R.transpose(); const Matrix& RT = R.GetTransposed(); float gradtheta = 0, gradv = 0; const auto& neighbors = agent->getNeighborSubsetInRange(range_); ... ...