142 lines
2.9 KiB
C++
142 lines
2.9 KiB
C++
#include "precompile.h"
|
|
|
|
#include <math.h>
|
|
|
|
#if 1
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/vec2.hpp>
|
|
#else
|
|
#include <Eigen/Core>
|
|
#include <Eigen/Dense>
|
|
#endif
|
|
|
|
#include "cs_proto.pb.h"
|
|
|
|
const Vector2D Vector2D::UP = Vector2D(0.0f, 1.0f);
|
|
const Vector2D Vector2D::RIGHT = Vector2D(1.0f, 0.0f);
|
|
const Vector2D Vector2D::DOWN = Vector2D(0.0f, -1.0f);
|
|
const Vector2D Vector2D::LEFT = Vector2D(-1.0f, 0.0f);
|
|
|
|
void Vector2D::ToPB(cs::MFVector2D* pb_obj)
|
|
{
|
|
pb_obj->set_x(x);
|
|
pb_obj->set_y(y);
|
|
}
|
|
|
|
void Vector2D::FromPB(const cs::MFVector2D* pb_obj)
|
|
{
|
|
x = pb_obj->x();
|
|
y = pb_obj->y();
|
|
}
|
|
|
|
void Vector2D::Normalize()
|
|
{
|
|
#if 1
|
|
glm::vec2 v = glm::normalize(glm::vec2(x, y));
|
|
assert(!isnan(v[0]));
|
|
assert(!isnan(v[1]));
|
|
x = v[0];
|
|
y = v[1];
|
|
#else
|
|
Eigen::Vector2f v(x, y);
|
|
v.normalize();
|
|
x = v[0];
|
|
y = v[1];
|
|
#endif
|
|
}
|
|
|
|
bool Vector2D::operator == (const Vector2D& b) const
|
|
{
|
|
return std::abs(x - b.x) < 0.01f && std::abs(y - b.y) < 0.01f;
|
|
}
|
|
|
|
Vector2D Vector2D::operator + (const Vector2D& b) const
|
|
{
|
|
#if 1
|
|
glm::vec2 v = glm::vec2(x, y) + glm::vec2(b.x, b.y);
|
|
return Vector2D(v[0], v[1]);
|
|
#else
|
|
Eigen::Vector2f v = Eigen::Vector2f(x, y) + Eigen::Vector2f(b.x, b.y);
|
|
return Vector2D(v[0], v[1]);
|
|
#endif
|
|
}
|
|
|
|
Vector2D Vector2D::operator - (const Vector2D& b) const
|
|
{
|
|
#if 1
|
|
glm::vec2 v = glm::vec2(x, y) - glm::vec2(b.x, b.y);
|
|
return Vector2D(v[0], v[1]);
|
|
#else
|
|
Eigen::Vector2f v = Eigen::Vector2f(x, y) - Eigen::Vector2f(b.x, b.y);
|
|
return Vector2D(v[0], v[1]);
|
|
#endif
|
|
}
|
|
|
|
Vector2D Vector2D::operator * (float scale) const
|
|
{
|
|
#if 1
|
|
glm::vec2 v = glm::vec2(x, y) * scale;
|
|
return Vector2D(v[0], v[1]);
|
|
#else
|
|
Eigen::Vector2f v = Eigen::Vector2f(x, y) * scale;
|
|
return Vector2D(v[0], v[1]);
|
|
#endif
|
|
}
|
|
|
|
Vector2D Vector2D::operator / (float scale) const
|
|
{
|
|
#if 1
|
|
glm::vec2 v = glm::vec2(x, y) / scale;
|
|
return Vector2D(v[0], v[1]);
|
|
#else
|
|
Eigen::Vector2f v = Eigen::Vector2f(x, y) * (1.0 / scale);
|
|
return Vector2D(v[0], v[1]);
|
|
#endif
|
|
}
|
|
|
|
#if 1
|
|
#include <Eigen/Core>
|
|
#include <Eigen/Dense>
|
|
#endif
|
|
|
|
void Vector2D::Rotate(float angle)
|
|
{
|
|
Eigen::Vector3f v(x, y, 0);
|
|
v = Eigen::AngleAxisf(angle * 3.1415926f, Eigen::Vector3f::UnitZ()) * v;
|
|
x = v[0];
|
|
y = v[1];
|
|
}
|
|
|
|
float Vector2D::CalcAngle(const Vector2D& b)
|
|
{
|
|
float a1 = acos(Dot(b) / Norm() / b.Norm());
|
|
float a2 = atan2(y, x);
|
|
float a3 = atan2(y, x) / 0.017 - 90.0f;
|
|
#if 0
|
|
return a2;
|
|
#else
|
|
bool at_right_side = Vector2D::RIGHT.Dot(*this) > 0.0001f;
|
|
if (at_right_side) {
|
|
a1 = -a1;
|
|
}
|
|
return a1 / 3.1415926f;
|
|
// return a3 / 360.0f;
|
|
#endif
|
|
}
|
|
|
|
Vector2D Vector2D::Perp()
|
|
{
|
|
return Vector2D(y, -x);
|
|
}
|
|
|
|
float Vector2D::Dot(const Vector2D& v) const
|
|
{
|
|
return x*v.x + y*v.y;
|
|
}
|
|
|
|
float Vector2D::Norm() const
|
|
{
|
|
return fabs(sqrt(x*x + y*y));
|
|
}
|