Test suite cleanups, convert Coord2D to Vector<T, 2>.
- The standard SimpleSuite setup now include flags to suppress printing the report in addition to silencing the test runs. This is useful in automated testing. - Point2D and Polar2D in Coord2D have been converted from custom types to Vector<int, 2> and Vector<double, 2>, respectively.
This commit is contained in:
@@ -28,6 +28,7 @@
|
||||
|
||||
#include <scmp/Math.h>
|
||||
#include <scmp/geom/Coord2D.h>
|
||||
#include <scmp/geom/Vector.h>
|
||||
|
||||
|
||||
// coord2d.cpp contains 2D geometric functions and data structures, such as
|
||||
@@ -43,19 +44,52 @@ namespace geom {
|
||||
// Point2D
|
||||
|
||||
|
||||
Point2D::Point2D() : x(0), y(0) {}
|
||||
Point2D::Point2D() : Vector<int, 2>{0, 0}
|
||||
{
|
||||
|
||||
Point2D::Point2D(int _x, int _y) : x(_x), y(_y) {}
|
||||
};
|
||||
|
||||
Point2D::Point2D(int _x, int _y) : Vector<int, 2>{_x, _y}
|
||||
{}
|
||||
|
||||
Point2D::Point2D(const Polar2D &pol)
|
||||
: x(std::rint(std::cos(pol.theta) * pol.r)),
|
||||
y(std::rint(std::sin(pol.theta) * pol.r)) {}
|
||||
: Vector<int, 2>{static_cast<int>(std::rint(std::cos(pol.Theta()) * pol.R())),
|
||||
static_cast<int>(std::rint(std::sin(pol.Theta()) * pol.R()))}
|
||||
{}
|
||||
|
||||
|
||||
std::ostream&
|
||||
operator<<(std::ostream& outs, const Point2D& pt)
|
||||
int
|
||||
Point2D::X() const
|
||||
{
|
||||
outs << "(" << std::to_string(pt.x) << ", " << std::to_string(pt.y) << ")";
|
||||
return this->at(0);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Point2D::X(int _x)
|
||||
{
|
||||
this->Set(0, _x);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
Point2D::Y() const
|
||||
{
|
||||
return this->at(1);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Point2D::Y(int _y)
|
||||
{
|
||||
this->Set(1, _y);
|
||||
}
|
||||
|
||||
|
||||
std::ostream &
|
||||
operator<<(std::ostream &outs, const Point2D &pt)
|
||||
{
|
||||
outs << "(" << std::to_string(pt[0]) << ", " << std::to_string(pt[1]) << ")";
|
||||
return outs;
|
||||
}
|
||||
|
||||
@@ -63,51 +97,43 @@ operator<<(std::ostream& outs, const Point2D& pt)
|
||||
std::string
|
||||
Point2D::ToString()
|
||||
{
|
||||
return "(" + std::to_string(x) + ", " + std::to_string(y) + ")";
|
||||
return "(" + std::to_string(this->X()) + ", " + std::to_string(this->Y()) + ")";
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Point2D::ToPolar(Polar2D& pol)
|
||||
Point2D::ToPolar(Polar2D &pol)
|
||||
{
|
||||
pol.r = std::sqrt((x * x) + (y * y));
|
||||
pol.theta = std::atan2(y, x);
|
||||
pol.R(std::sqrt(this->X() * this->X() + this->Y() * this->Y()));
|
||||
pol.Theta(std::atan2(this->Y(), this->X()));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Point2D::Rotate(Point2D& pt, double theta)
|
||||
Point2D::Rotate(Point2D &pt, double theta)
|
||||
{
|
||||
Polar2D pol(*this);
|
||||
Polar2D pol(*this);
|
||||
pol.Rotate(pol, theta);
|
||||
pol.ToPoint(pt);
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
Point2D::operator==(const Point2D& rhs) const
|
||||
{
|
||||
return (x == rhs.x) && (y == rhs.y);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Point2D::Translate(const Point2D& origin, Point2D &translated)
|
||||
Point2D::Translate(const Point2D &origin, Point2D &translated)
|
||||
{
|
||||
translated.x = origin.x + x;
|
||||
translated.y = origin.y + y;
|
||||
translated.X(origin.X() + this->X());
|
||||
translated.Y(origin.Y() + this->Y());
|
||||
}
|
||||
|
||||
|
||||
std::vector<Point2D>
|
||||
Point2D::Rotate(std::vector<Polar2D> vertices, double theta)
|
||||
{
|
||||
std::vector<Point2D> rotated;
|
||||
std::vector<Point2D> rotated;
|
||||
|
||||
for (auto& v : vertices) {
|
||||
Point2D p;
|
||||
for (auto &v: vertices) {
|
||||
Point2D p;
|
||||
v.RotateAround(*this, p, theta);
|
||||
rotated.push_back(p) ;
|
||||
rotated.push_back(p);
|
||||
}
|
||||
|
||||
return rotated;
|
||||
@@ -115,70 +141,105 @@ Point2D::Rotate(std::vector<Polar2D> vertices, double theta)
|
||||
|
||||
|
||||
int
|
||||
Point2D::Distance(const Point2D& other)
|
||||
Point2D::Distance(const Point2D& other) const
|
||||
{
|
||||
auto dx = other.x - x;
|
||||
auto dy = other.y - y;
|
||||
return std::sqrt(dx * dx + dy + dy);
|
||||
auto dx = other.X() - this->X();
|
||||
auto dy = other.Y() - this->Y();
|
||||
return static_cast<int>(std::rint(std::sqrt(dx * dx + dy * dy)));
|
||||
}
|
||||
|
||||
|
||||
// Polar2D
|
||||
|
||||
Polar2D::Polar2D() : Vector<double, 2>{0.0, 0.0} {};
|
||||
|
||||
Polar2D::Polar2D(double _r, double _theta) : Vector<double, 2>{_r, _theta}
|
||||
{}
|
||||
|
||||
Polar2D::Polar2D(const Point2D &pt)
|
||||
: r(std::sqrt((pt.x * pt.x) + (pt.y * pt.y))),
|
||||
theta(std::atan2(pt.y, pt.x)) {}
|
||||
: Vector<double, 2>{std::sqrt((pt.X() * pt.X()) + (pt.Y() * pt.Y())),
|
||||
std::atan2(pt.Y(), pt.X())}
|
||||
{}
|
||||
|
||||
|
||||
double
|
||||
Polar2D::R() const
|
||||
{
|
||||
return this->at(0);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Polar2D::ToPoint(Point2D& pt)
|
||||
Polar2D::R(const double _r)
|
||||
{
|
||||
pt.y = std::rint(std::sin(theta) * r);
|
||||
pt.x = std::rint(std::cos(theta) * r);
|
||||
this->Set(0, _r);
|
||||
}
|
||||
|
||||
|
||||
double
|
||||
Polar2D::Theta() const
|
||||
{
|
||||
return this->at(1);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Polar2D::Theta(const double _theta)
|
||||
{
|
||||
this->Set(1, _theta);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Polar2D::ToPoint(Point2D &pt)
|
||||
{
|
||||
pt.Y(std::rint(std::sin(this->Theta()) * this->R()));
|
||||
pt.X(std::rint(std::cos(this->Theta()) * this->R()));
|
||||
}
|
||||
|
||||
|
||||
std::string
|
||||
Polar2D::ToString()
|
||||
{
|
||||
return "(" + std::to_string(r) + ", " + std::to_string(theta) + ")";
|
||||
return "(" + std::to_string(this->R()) +
|
||||
", " + std::to_string(this->Theta()) + ")";
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Polar2D::Rotate(Polar2D& rot, double delta)
|
||||
Polar2D::Rotate(Polar2D &rot, double delta)
|
||||
{
|
||||
rot.r = r;
|
||||
rot.theta = RotateRadians(theta, delta);
|
||||
rot.R(this->R());
|
||||
rot.Theta(RotateRadians(this->Theta(), delta));
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
Polar2D::operator==(const Polar2D& rhs) const
|
||||
Polar2D::operator==(const Polar2D &rhs) const
|
||||
{
|
||||
static double eps = 0.0;
|
||||
if (eps == 0.0) {
|
||||
scmp::DefaultEpsilon(eps);
|
||||
}
|
||||
return scmp::WithinTolerance(r, rhs.r, eps) &&
|
||||
scmp::WithinTolerance(theta, rhs.theta, eps);
|
||||
return scmp::WithinTolerance(this->R(), rhs.R(), eps) &&
|
||||
scmp::WithinTolerance(this->Theta(), rhs.Theta(), eps);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Polar2D::RotateAround(const Point2D &origin, Point2D &point, double delta)
|
||||
{
|
||||
Polar2D rot;
|
||||
Polar2D rot;
|
||||
this->Rotate(rot, delta);
|
||||
rot.ToPoint(point);
|
||||
rot.ToPoint(point);
|
||||
point.Translate(origin, point);
|
||||
}
|
||||
|
||||
|
||||
std::ostream&
|
||||
operator<<(std::ostream& outs, const Polar2D& pol)
|
||||
std::ostream &
|
||||
operator<<(std::ostream &outs, const Polar2D &pol)
|
||||
{
|
||||
outs << "(" << pol.r << ", " << pol.theta << ")";
|
||||
outs << "(" << pol.R() << ", " << pol.Theta() << ")";
|
||||
return outs;
|
||||
}
|
||||
|
||||
|
||||
@@ -131,18 +131,25 @@ static constexpr float Epsilon_float = 0.0001;
|
||||
|
||||
|
||||
void
|
||||
DefaultEpsilon(double &epsilon)
|
||||
DefaultEpsilon(double& epsilon)
|
||||
{
|
||||
epsilon = Epsilon_double;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
DefaultEpsilon(float &epsilon)
|
||||
DefaultEpsilon(float& epsilon)
|
||||
{
|
||||
epsilon = Epsilon_float;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
DefaultEpsilon(int& epsilon)
|
||||
{
|
||||
epsilon = 0;
|
||||
}
|
||||
|
||||
|
||||
} // namespace scmp
|
||||
|
||||
|
||||
Reference in New Issue
Block a user